Firestore arrayUnion [英] Firestore arrayUnion

查看:54
本文介绍了Firestore arrayUnion的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm building a basic CRUD app with vue.js and firebase. I'm trying to build out a favorites functionality and have ran into a persistent problem storing the data.

使用时,单击添加到收藏夹"按钮时,我试图将文档ID添加到用户个人资料"文档中的数组中.这是代码:

When a using clicks the add to favorites button I'm attempting to add the document id to an array in a "user profile" document. Here's the code:

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");
  }
}

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

I get the following error in the console:

Uncaught TypeError: Cannot read property 'arrayUnion' of undefined
    at addNewUserAction

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

I have firebase and the db ref imported, and have ran a bogus .set() through each reference to confirm they are pointing at the right spots. I also already have the arrays created in 'userProfile' document.

firebase安装是最新的,就像上周一样,所以我应该在我的构建中使用该功能.

The firebase install is fresh, like last week fresh, so I should have the function in my build.

似乎arrayUnion不能正常工作.有任何想法吗?其他的Firestore函数可以正常工作,所以我不确定.我是不是在偷东西?谢谢

Seems that arrayUnion is just not working. Any ideas? The other firestore functions are workin so I'm not sure. Am I blatenly missing something? Thanks

推荐答案

如果您使用的是Admin SDK

我正在尝试一些与此类似的随机错误.原来是因为我使用的是Firebase admin sdk,与Web SDK文档相比,它需要稍作更改.如果您使用的是Firebase管理员,请替换

I was having some random errors similar to this, among others as I was experimenting. It turns out it was because I was using firebase admin sdk which requires a slight change compared to the web SDK documentation. If you are using firebase admin, replace

firebase.firestore.FieldValue.arrayUnion(...

使用

admin.firestore.FieldValue.arrayUnion(...

这篇关于Firestore arrayUnion的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆