I'm trying to get all the posts from Firebase and add them to an array, but I'm getting the following error:
Uncaught (in promise) TypeError: Cannot read property 'blogItems' of undefined
The following is the script:
export default { data(){ return{ blogItems: [] } }, mounted(){ this.getPosts(); }, methods:{ getPosts(){ database.collection('blog').doc('yP6aYXvisFbTsqtQ3MEfuyz6xYE3').collection('posts').get().then(snapshot =>{ const posts = snapshot.docs.map(doc => doc.data()) posts.forEach(function(post){ this.blogItems.push(post.content) }) }) }, } }
In your case, change
this
inthis.blogItems
toposts
.You have two solutions
Store
this
into a variable and use itUse arrow functions