小编Tjb*_*187的帖子

Firestore arrayUnion

我正在使用vue.js和firebase构建一个基本的CRUD应用程序。我正在尝试建立收藏夹功能,并遇到了存储数据的持久性问题。

当使用单击添加到收藏夹按钮时,我正在尝试将文档ID添加到“用户个人资料”文档中的数组。这是代码:

export function addNewUserAction (type, key, userID){
  console.log(userID);
  console.log(key);

  firebase.firestore().collection('userActions').add({
    type: type,
    listing: key,
    user: userID,
    time: Date.now()
  })
  if(type === 'favorite'){
    var sendfav = db.collection('userProfiles').doc(userID).update({
      favs: firebase.firestore.FieldValue.arrayUnion(key)
    });
  }else if(type === 'xout'){
    var sendxout = db.collection('userProfiles').doc(userID).update({
      xouts: firebase.firestore.FieldValue.arrayUnion(key)
    });
  }else{
    console.error(type + " is not a user action");
  }
}
Run Code Online (Sandbox Code Playgroud)

我在控制台中收到以下错误:

Uncaught TypeError: Cannot read property 'arrayUnion' of undefined
    at addNewUserAction
Run Code Online (Sandbox Code Playgroud)

我已经导入了Firebase和db ref,并通过每个引用运行了一个假的.set(),以确认它们指向的位置正确。我已经在'userProfile'文档中创建了数组。

Firebase安装是最新的,就像上周一样,因此我应该在构建中具有该功能。

似乎arrayUnion不能正常工作。有任何想法吗?其他的Firestore函数正在运行,因此我不确定。我会很无聊吗?谢谢

javascript firebase vue.js google-cloud-firestore

2
推荐指数
3
解决办法
3898
查看次数