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

问题描述

我正在使用 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 阵列联合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-31 20:51