3

I've seen some similar questions but they don't seem to match my situation.

When I log this.$store.state.account I get the expected result


    {__ob__: Nt}
       user: Object
          favorites_playlist: (...)
          firebaseID: (...)
          spotifyID: (...)

However, when I console.log(this.$store.state.account.user) the user object disappears! All of the nested properties inside user return undefined though they log perfectly fine above

console.log(this.$store.state.account.user)

{__ob__: Nt}
   __ob__: Nt {value: {…}, dep: vt, vmCount: 0}
   __proto__: Object

This is the method inside my component calling the object

        async connectToSpotify() {
            console.log("User Profile: ", this.user)
            var firebaseID = await this.$store.dispatch("signInToFirebase")
  
            var authID = await this.$store.dispatch('connectToSpotify')
            Vue.set(this.$store.state.spotify, "authId", authID)

            var userProfile = await this.$store.dispatch("fetchUserDataFromSpotify", authID)
            userProfile["firebaseID"] = firebaseID

    
            this.$store.dispatch("bindCurrentUser", userProfile.spotifyID)

            console.log("this.$store.state")
            console.log(this.$store.state);
            console.log("this.$store.state.account")
            console.log(this.$store.state.account);
            console.log("this.$store.state.account.user")
            console.log(this.$store.state.account.user);
            console.log("this.$store.state.account.user.favorites_playlist")
            console.log(this.$store.state.account.user.favorites_playlist);
            // console.log(this.$store.state.account.user.firebaseID);
             var favorites_playlist = this.$store.state.account.user.favorites_playlist


            var playlistID = await this.$store.dispatch("createFavoritesPlaylist", [authID, userProfile.spotifyID, favorites_playlist])
            console.log(`PlaylistID: ${playlistID}`);

            userProfile["favorites_playlist"] = playlistID
            console.log(this.$store);
            return db.ref(`users/${userProfile.spotifyID}`).update(userProfile)
            
        },

this is the action inside my accounts module that binds the user to firebase

const state = () => ({
    //user: { voted_tracks: {}, favorited_tracks: {}, favorites_playlist: null, authID: null}
    user: {},
})
const actions = {
    bindCurrentUser: firebaseAction(({ bindFirebaseRef }, id) => {
      return bindFirebaseRef('user', db.ref('users').child(id))
    }),
}

Not sure what further information would be relevant aside that this.$store.state.account.user is binded via Vuexfire to a database reference. The store is injected into the root component

3
  • 1
    stackoverflow.com/questions/61011168/… Commented Dec 21, 2020 at 5:53
  • 1
    This is awesome! seems like my data wasn't loaded by the time I needed it, will see how I can refactor my code to account for this and check back in Commented Dec 21, 2020 at 5:59
  • Apologies if this is a dumb question but you don't mean literally add return before firebaseAction do you? it returns an error, how should this line be edited? return bindFirebaseRef('user', db.ref('users').child(id)) Commented Dec 21, 2020 at 6:19

1 Answer 1

1

Your data comes in after the console.log. The console updates object/array logs with current values when you click, but can't do that with primitives. See this answer for more detail.

It should be enough to await the firebaseAction:

await this.$store.dispatch("bindCurrentUser", userProfile.spotifyID)
bindCurrentUser: firebaseAction(({ bindFirebaseRef }, id) => {
   console.log("account.bindCurrentUser() called");
   return bindFirebaseRef('user', db.ref(`users/${id}`)).then(() => {
       console.log("account.bindCurrentUser() -- complete")
   }).catch((err) => {console.log(err)})
}),
Sign up to request clarification or add additional context in comments.

7 Comments

Hopefully firebase isn't doing too much magic with firebaseAction. I don't actually use firebase so I don't know what it's doing to be sure that this works
Updated my code to include your recommendations here. It doesn't seem like the the await code is working. I added return bindFirebaseRef('user', db.ref('user').child(id)).then(() => console.log("account.bindCurrentUser() -- complete")) and the log statement is never printed
I also made sure to declare async bindCurrentUser() to make sure that wasn't the issue
Figured it out!
Ok that means firebaseAction returns a function and not a promise (which makes sense in hindsight), that would explain my misconception. You're welcome, glad you got it figured out
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.