【问题标题】:Firebase Cloud Firestore error + Vue + Vuex (firebase.firestore.QuerySnapshot)Firebase Cloud Firestore 错误 + Vue + Vuex (firebase.firestore.QuerySnapshot)
【发布时间】:2018-12-30 01:02:06
【问题描述】:

我发现 docChanges 看起来有些问题,它是一个函数,但是当我写 docChanges().doc.data().userId 时出现此错误 store.js?3bf3:21 Uncaught TypeError: Cannot read property 'doc' of undefined

::更新

我想登录/注册,但我现在收到此错误 一切正常,但登录功能中的this.$router.push('/dashboard') 没有。为什么?

Store.js

state: {
  currentUser: null,
  userProfile: {},
  posts: [],
  hiddenPosts: []
},
actions: {
    clearData({ commit }) {
        commit('setCurrentUser', null)
        commit('setUserProfile', {})
        commit('setPosts', null)
    },
    fetchUserProfile({ commit, state }) {
        fb.usersCollection.doc(state.currentUser.uid).get().then(res => {
            commit('setUserProfile', res.data())
        }).catch(err => {
            console.log(err)
        })
    }
},
mutations: {
    setCurrentUser(state, val) {
        state.currentUser = val
    },
    setUserProfile(state, val) {
        state.userProfile = val
    },
    setPosts(state, val) {
        if (val) {
            state.posts = val
        } else {
            state.posts = []
        }
    },
    setHiddenPosts(state, val) {
        if (val) {
            if (!state.hiddenPosts.some(x => x.id === val.id)) {
                state.hiddenPosts.unshift(val)
            }
        } else {
            state.hiddenPosts = []
        }
    }
}

登录错误码

Login.vue 组件

             <form v-if="showLoginForm" @submit.prevent>
                <h1>Welcome Back</h1>

                <label for="email1">Email</label>
                <input v-model.trim="loginForm.email" type="text" 
                 placeholder="you@email.com" id="email1" />

                <label for="password1">Password</label>
                <input v-model.trim="loginForm.password" type="password" 
                  placeholder="******" id="password1" />

                <button @click="login" class="button">Log In</button>
             </form>

        data() {
           return {
              loginForm: {
                email: '',
                password: ''
              }
           }
        },
        methods: {
           login() {
            this.performingRequest = true

            fb.auth.signInWithEmailAndPassword(this.loginForm.email, 
              this.loginForm.password).then(user => {
                this.$store.commit('setCurrentUser', user)
                this.$store.dispatch('fetchUserProfile')
                this.performingRequest = false
                this.$router.push('/dashboard')
            }).catch(err => {
                console.log(err)
                this.performingRequest = false
                this.errorMsg = err.message
            })
          },
        }

【问题讨论】:

    标签: javascript firebase vue.js google-cloud-firestore


    【解决方案1】:

    你正在调用querySnapshot.docChanges,就好像它是一个数组:

    querySnapshot.docChanges.length...
    querySnapshot.docChanges[0]...
    

    但是querySnapshot.docChanges是一个函数,所以你需要先调用这个函数,然后才能得到一个数组:

    querySnapshot.docChanges().length...
    querySnapshot.docChanges()[0]...
    

    【讨论】:

    • 我花了很长时间试图弄清楚我需要该方法之后的[0]。谢谢!
    猜你喜欢
    • 2019-08-30
    • 2022-01-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-16
    • 1970-01-01
    • 2018-05-13
    相关资源
    最近更新 更多