我发现docchanges有问题,这是一个函数,但是当我写docChanges().doc.data().userId
时,会出现这个错误store.js?3bf3:21 Uncaught TypeError: Cannot read property 'doc' of undefined
●更新
我想登录/注册,但我现在收到这个错误
一切正常,但登录函数中的this.$router.push('/dashboard')
不正常。为什么?
商店.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 = []
}
}
}
Error code of Login
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="[email protected]" 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
})
},
}
最佳答案
您调用querySnapshot.docChanges
时就好像它是一个数组:
querySnapshot.docChanges.length...
querySnapshot.docChanges[0]...
但是
querySnapshot.docChanges
是一个函数,所以在返回数组之前需要先调用该函数:querySnapshot.docChanges().length...
querySnapshot.docChanges()[0]...