Javascript 如何在firestore中创建阵列?
在Javascript 如何在firestore中创建阵列?,javascript,firebase,vue.js,google-cloud-firestore,Javascript,Firebase,Vue.js,Google Cloud Firestore,在Firebase Firestore中创建数组需要哪些代码 我想存储userDetails: userName: this.userProfile.name, userImage: this.userProfile.image, userId: this.userProfile.userId 作为Firestore中的数组 Vue组件: ... data () { return { postDetails: { createdOn: new Date(),
Firebase Firestore
中创建数组需要哪些代码
我想存储userDetails
:
userName: this.userProfile.name,
userImage: this.userProfile.image,
userId: this.userProfile.userId
作为Firestore中的数组
Vue
组件:
...
data () {
return {
postDetails: {
createdOn: new Date(),
content: '',
userId: null,
image: null,
comments: 0,
likes: 0,
userDetails: []
}
userData: [
{ userName: this.userProfile.name },
{ userImage: this.userProfile.image},
{ userId: this.userProfile.userId }
}
},
methods: {
createPost () {
fb.postsCollection.add({
createdOn: this.postDetails.createdOn,
content: this.postDetails.content,
userId: this.currentUser.uid,
image: this.postDetails.image,
comments: this.postDetails.comments,
likes: this.postDetails.likes
userData: this.userData
}).then(ref => {
this.post.content = ''
this.$router.push('/dashboard')
}).catch(err => {
console.log(err)
})
}
}
我看到您正在修改Firestore和Vue.js教程中的代码: 实际上,在本教程中,
userProfile
数据来自vue存储
computed: {
...mapState(['userProfile', 'currentUser', 'posts'])
},
您的问题中缺少这部分代码,但这很可能是问题的原因:在Vue.js组件中,您尝试在数据中使用计算属性,但这是不可能的,“因为组件创建时间:数据在计算属性之前计算”
看到和
只需按如下方式直接访问计算的属性,它就会工作:userData
将存储为数组
methods: {
createPost () {
fb.postsCollection.add({
createdOn: this.postDetails.createdOn,
content: this.postDetails.content,
userId: this.currentUser.uid,
image: this.postDetails.image,
comments: this.postDetails.comments,
likes: this.postDetails.likes
userData: [
{ userName: this.userProfile.name },
{ userImage: this.userProfile.image},
{ userId: this.userProfile.userId }
]
}).then(ref => {
this.post.content = ''
this.$router.push('/dashboard')
}).catch(err => {
console.log(err)
})
}
}
文档中已经有两个数组:标记和用户数据。我根本不清楚你遇到了什么问题是的,但是我在firebase控制台中手动添加了这两个选项我不知道如何使用code实现这一点您是否尝试过将填充了一些数据的JavaScript数组放入您的代码中?是的,但在firestore中它的输出与普通字符串类似谢谢,但为什么不起作用:''userData:[{userName:this.userProfile.name,userImage:this.userProfile.image,userId:this.currentUser.userId}]`I希望索引0中的所有3个取决于您在教程中的位置,
userImage
和name
的值可能未定义。请仅尝试userId:this.currentUser.userId
我发现了错误,它无法与`currentUser`一起工作,仅与userProfile一起工作。因此一切正常?