Vuex photoURL and displayName have been passed null to SetUser

假装没事ソ 提交于 2020-03-12 06:26:33

问题


My setUser

setUser(state, payload) {
   state.user = {...payload}
}

my payload in setUser for random data is

user === payload{"uid":"pQOQL9AqMHNsozDE2EFGbMfHZlt1","refreshToken":"AEu4IL3c4doh1ON1ywqNEIXPjijktxAyQsYusC5twuvM61bHK6PpLHENyqKRKGCvNPR5IxBRC7JLQhkjv1qqiVUPdatRVM2Q8VdBCnvxyKkBjOEt_kM6bHCiJI6cdESdmFWZf2B7EjG9MwUJ7l8ASOpdbQLLVs9NtuW94dpNg1dkQShtUXB-sVCafvgtSnluGyZSWGhkt8uJ","photoURL":null,"displayName":null,"email":"yyy@test.com"}

My signUserUp

signUserUp(context, payload) {
  //name , email , and password are in payload
  context.commit("setLoading", true);
  context.commit("clearError");

  context.commit("setUserAvatar")

  firebase
    .auth()
    .createUserWithEmailAndPassword(payload.email, payload.password)
    .then((data) => {
      data.user.updateProfile({
        displayName: payload.name ,
        photoURL: 'https://avataaars.io/?avatarStyle=Circle&topType=ShortHairDreads01&accessoriesType=Prescription01&hairColor=BlondeGolden&facialHairType=BeardMedium&facialHairColor=BrownDark&clotheType=Hoodie&clotheColor=Gray01&eyeType=Squint&eyebrowType=AngryNatural&mouthType=Sad&skinColor=Light'
     })
     return data
    })
    .then((data) => {
      context.commit("setLoading", false);

    db.collection("profilesInfo")
        .add({
          id: data.user.uid,
          registeredMeetups: []
        })
        .then(function() {
          context.commit("setProfilesInfo", 
             {    
                id: data.user.uid,
                registeredMeetups: []
              }
              )
          console.log("Document successfully written!");
      })
      .catch(function(error) {
          console.error("Error writing document: ", error);
      });
      context.commit("setUser", {
        name: payload.name ,
        id: data.user.uid,
        photoURL: 'https://avataaars.io/?avatarStyle=Circle&topType=ShortHairDreads01&accessoriesType=Prescription01&hairColor=BlondeGolden&facialHairType=BeardMedium&facialHairColor=BrownDark&clotheType=Hoodie&clotheColor=Gray01&eyeType=Squint&eyebrowType=AngryNatural&mouthType=Sad&skinColor=Light',
        email: data.user.email
      })
      router.push("/");
    })
    .catch(function(error) {
      // Handle Errors here.
      context.commit("setLoading", false);
      context.commit("setError", error);
    });


},

In signUserUp

an user will be created and upon creation value of photoURL and displayName get updated by updateProfile and after that db is connected again in order to create profilesinfo related to the user which is about showing what meetup groups the user is registered already

It works when in my setUser, I set the value of payload inside setUser and photoURL and displayName will be populated right and I can use it in my profile vue component

I debugged everything inside the console and I cannot understand why this part does not pass photoURL and displayName right

   context.commit("setUser", {
            name: payload.name ,
            id: data.user.uid,
            photoURL: 'https://avataaars.io/?avatarStyle=Circle&topType=ShortHairDreads01&accessoriesType=Prescription01&hairColor=BlondeGolden&facialHairType=BeardMedium&facialHairColor=BrownDark&clotheType=Hoodie&clotheColor=Gray01&eyeType=Squint&eyebrowType=AngryNatural&mouthType=Sad&skinColor=Light',
            email: data.user.email
          })

Note: I cleared my storage very often

or In second thought, can my issue is because of using persistent data?

const vuexLocalStorage = new VuexPersist({
  key: 'devmeetup-it', // The key to store the state on in the storage provider.
  storage: window.localStorage, // or window.sessionStorage or localForage
  // Function that passes the state and returns the state with only the objects you want to store.

  //  reducer: state => ({
  //   keepLoadedMeetups : store.getters.loadedMeetups, 
  //   keepUser: store.getters.user,
  //   keepProfilesInfo: state.profilesInfo
  //   // getRidOfThisModule: state.getRidOfThisModule (No one likes it.)
  // })
  // Function that passes a mutation and lets you decide if it should update the state in localStorage.
  // filter: mutation => (true)
})

Note: after reloading and visiting other pages and come back to profile page, the photo is shown I guess the info became available then?

My Profile.vue

<template  >
<div>
  <v-card
    class="mx-auto"
    max-width="434"    
    tile
  >
   <v-img
      height="100%"
      src="https://cdn.vuetifyjs.com/images/cards/server-room.jpg"
    > 
      <v-row
        align="end"
        class="fill-height"
      >
        <v-col
          align-self="start"
          class="pa-0"
          cols="12"
        >
          <v-avatar
            class="profile"

            size="164"
            tile
          >
          <img :src="imgUrl" alt="">

          </v-avatar>
        </v-col>
        <v-col class="py-0">
          <v-list-item
            color="rgba(0, 0, 0, .4)"
            dark
          >
            <v-list-item-content>
              <v-list-item-title class="title">Name: {{owner_name}}</v-list-item-title>
              <v-list-item-subtitle>Email: {{user_info.email}}</v-list-item-subtitle>
              <template v-if="meetups.length> 0 ">
                <v-list-item-subtitle>Meetup organizer :</v-list-item-subtitle>
                  <v-card color="rgba(255, 0, 0, 0.5)">
                    <ol start="1" v-for="(meetup,i) in meetups" v-bind:key="i">
                        <span >{{i+1}}. {{meetup.title}}</span>
                    </ol>              
                </v-card>
              </template>
              <template>
                <div v-if="registeredMeetups.length> 0 ">
                  <v-list-item-subtitle>Meetup Registred :</v-list-item-subtitle>
                  <v-card color="rgba(255, 0, 0, 0.5)">
                    <ol start="1" v-for="(meetup,i) in registeredMeetups" v-bind:key="i">
                        <span >{{i+1}}. {{meetup.title}}</span>
                    </ol>              
                  </v-card>
                </div>
              </template>

            </v-list-item-content>
          </v-list-item>

        </v-col>
      </v-row>
    </v-img>

  </v-card>
  <!--<h3>orginzer meetups:  {{this.meetups}}</h3>
  <h3>registered meetups: {{this.registeredMeetups}}</h3>
  <h3>All the meetups: {{this.$store.getters.loadedMeetups}} </h3> 
  <div style="word-wrap: break-word"> {{imgUrl}} </div>-->

  </div>
</template>
<script>
import {mapState} from 'vuex' 
export default {
    data(){
      return {
        imgUrl: this.$store.state.user.photoURL
      }
    },
    created(){
      // this.$store.subscribe((mutation, state) => {
      //   if (mutation.type === "setUserAvatar") {
      //     //debugger; // eslint-disable-line no-debugger
      //     this.imgUrl = state.user.photoURL
      //   }
      // });
    },
    computed: {
        ...mapState({
             owner_name: state => state.user.displayName,
             user_info: state => state.user
        }),
        registeredMeetups(){
          let rm= this.$store.getters.currentUserProfileInfo.registeredMeetups
          let allm = this.$store.getters.loadedMeetups
          let meetupsInfo = []
          let i , j 
          console.log("rm and all meetups are " + JSON.stringify(allm))
          for (i = 0; i < rm.length; i++) {
            console.log("rm=" + rm[i].toString()  )
            for ( j = 0 ; j < allm.length; j++){
              console.log("lm= " + JSON.stringify(allm[j]))
                if(allm[j].id == rm[i].toString()) 
                  meetupsInfo.push(allm[j])
                  }
          }
           console.log("meetupsInfo " + JSON.stringify(this.$store.state.photoURL))
          return meetupsInfo
        },
        meetups(){
           return this.$store.getters.loadedMeetups
                       .filter( meetup => meetup.creatorId === this.$store.getters.user.uid )
        },
        profilesInfo(){
          //  let currentUserProfile = this.$store.state.profilesInfo
          //                 .find( userProfile =>
          //                        userProfile.id === this.$store.getters.user.uid )
           return this.$store.getters.currentUserProfileInfo       
        }


    }
}
</script>

or May be, using then clause inside another the clause will have different effect ? please take a look at my signUserUp then clauses.

my github repo github.com/KickButtowski80/devmeetup/tree/setting-avataaars

please if more info is needed let me know thank you


回答1:


I see there is some different parameter passed in setUser()

In your signUserUp(context, payload):

...
context.commit("setUser", {
      name: payload.name ,
      id: data.user.uid,
      photoURL: 'https://avataaars.io/?avatarStyle=Circle&topType=ShortHairDreads01&accessoriesType=Prescription01&hairColor=BlondeGolden&facialHairType=BeardMedium&facialHairColor=BrownDark&clotheType=Hoodie&clotheColor=Gray01&eyeType=Squint&eyebrowType=AngryNatural&mouthType=Sad&skinColor=Light',
      email: data.user.email
    });
router.push("/");
...

You also said :

It works when in my setUser, I set the value of payload inside setUser and photoURL and displayName will be populated right and I can use it in my profile vue component

Does it mean you put it in /store/index.js :

setUser(state, payload) {
  const {uid, refreshToken, photoURL, displayName, email} = payload;
  console.log('user === payload' + JSON.stringify(payload))
  console.log('payload detail info ' + payload.uid + " " + payload.refreshToken 
                           + " " + payload.photoURL + " " + payload.displayName + " " + payload.email )
  // payload = {
  //   displayName:"test7",
  //   email:"test7@test.com",
  //   photoURL:"https://avataaars.io/?avatarStyle=Circle&topType=ShortHairDreads01&accessoriesType=Prescription01&hairColor=BlondeGolden&facialHairType=BeardMedium&facialHairColor=BrownDark&clotheType=Hoodie&clotheColor=Gray01&eyeType=Squint&eyebrowType=AngryNatural&mouthType=Sad&skinColor=Light",
  //   refreshToken:"AEu4IL0tC9-fuEO-KZNwq953YDo2V7FBpjqB62FT6nXJ5d3r5u3Fzk1RYDzbjkO885rz0LrLyvIjHKHIDemiZsVPeio5XPXK5ntuRyFtLYcu-QOV4xnYYMn18mFxjo6P_TeqrnGIBuwpoto0ceTPxNfYFmedNyuxbNIU6MUVRp5WvnI7OWxVO5404RHIsnLrBsABoigDZgxs",
  //   uid:"XAhAqlBbs5VZCredSDqdWqKze6C3",
  // }
  state.user = {...{uid, refreshToken, photoURL, displayName, email}} 

So, if you uncomment the line it works ?

Try to pass all the params used with the same key:

...
context.commit("setUser", { 
      uid: data.user.uid,  // id => uid,
      displayName: payload.name ,  // name=> displayName
      refreshToken: 'your-token',
      photoURL: 'https://avataaars.io/?avatarStyle=Circle&topType=ShortHairDreads01&accessoriesType=Prescription01&hairColor=BlondeGolden&facialHairType=BeardMedium&facialHairColor=BrownDark&clotheType=Hoodie&clotheColor=Gray01&eyeType=Squint&eyebrowType=AngryNatural&mouthType=Sad&skinColor=Light',
      email: data.user.email
    });
router.push("/");
...

Does it work ?



来源:https://stackoverflow.com/questions/60572208/vuex-photourl-and-displayname-have-been-passed-null-to-setuser

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!