【发布时间】:2020-03-16 07:49:45
【问题描述】:
我正在尝试通过 nuxtServerInit 获取一些数据并将其保存在 state 中
存储/index.js
import { fireDb } from '~/plugins/firebase'
export const state = () => ({
posts: []
})
export const mutations = {
addPosts (state, post) {
state.posts.push(post)
console.log('mutation =>', state.posts.length)
}
}
export const actions = {
nuxtServerInit (state, ctx) {
fireDb.collection('posts').orderBy('timestamp', 'desc').limit(3).get().then((snapshot) => {
snapshot.forEach((doc) => {
state.commit('addPosts', doc.data())
})
console.log('action => ', state.posts.length)
})
}
}
当我运行此代码时,控制台输出是
mutation => 1
mutation => 2
mutation => 3
ERROR Cannot read property 'length' of undefined
而且 vue 开发工具也没有显示帖子 [] 中的数据。
我在这里错过了什么?
【问题讨论】: