如何使用Firestore中的对象数组更新字段中的特定对象? [英] How to update specific object in a Field with array of objects in Firestore?

查看:34
本文介绍了如何使用Firestore中的对象数组更新字段中的特定对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个 emojiCounter 字段,该字段用于存储像这样的对象数组

  emojiCounter:[{emoji:'haha',作者:'user1'},{emoji:'haha',作者:'user2'}] 

但是我试图弄清楚如果用户反复选择不同的表情符号原因如何更新对象,在这种情况下,我只需要通过 userName

解决方案

由于 emojiCounter 是对象的数组,因此您需要先阅读该数组,然后在其中进行更新前端,然后将其保存回Firestore.使用 array-union()方法不起作用,因为它是一个对象数组.

您可以使用以下代码进行验证.对象已添加,未更新.

  const db = firebase.firestore();const docRef = db.collection('testSO').doc('testSO');const emojiCounter = [{emoji:'haha',作者:'user1'},{emoji:'haha',作者:'user2'},];docRef.set({emojiCounter}).then(()=> {返回docRef.get();}).then((doc)=> {console.log('#1',doc.data().emojiCounter);返回docRef.update({emojiCounter:firebase.firestore.FieldValue.arrayUnion({表情符号:"hoho",创建人:"user1",}),});}).then(()=> {返回docRef.get();}).then((doc)=> {console.log('#2',doc.data().emojiCounter);返回docRef.update({emojiCounter:firebase.firestore.FieldValue.arrayUnion({表情符号:"hihi",创建人:"user2",}),});}).then(()=> {返回docRef.get();}).then((doc)=> {console.log('#3',doc.data().emojiCounter);}); 


请注意,您可能会遇到上述解决方案的问题,因为根据您的应用程序功能,它可能暗示 user1 可能会更改 emoji user2 .如果要避免这种情况,则应采用另一种方法.例如,允许用户仅更新其表情符号(例如,每位用户使用一个表情符号文档),并具有Cloud Function(云功能),该功能会在这些文档之一更新时触发,并会更新包含数组的中央文档.您应该使用交易来执行更新.>


此外,由于您使用的是一个包含所有用户表情符号的数组,因此请注意字段值的最大大小:1,048,487字节.

So I have a emojiCounter field which is used to store an array of objects like

emojiCounter : [{emoji : 'haha', by : 'user1'},{emoji : 'haha', by : 'user2'}]

But I am trying to figure out how to update an object if a user repeatedly selects different emoji cause in that case I only have to update the emoji by userName

解决方案

Since emojiCounter is an array of Objects, you need to first read the Array, update it in the frontend then save it back to Firestore. Using the array-union() method will not work, because it is an array of Objects.

You can verify that with the following code. The objects are added, not updated.

  const db = firebase.firestore();
  const docRef = db.collection('testSO').doc('testSO');

  const emojiCounter = [
    { emoji: 'haha', by: 'user1' },
    { emoji: 'haha', by: 'user2' },
  ];

  docRef
    .set({ emojiCounter })
    .then(() => {
      return docRef.get();
    })
    .then((doc) => {
      console.log('#1', doc.data().emojiCounter);

      return docRef.update({
        emojiCounter: firebase.firestore.FieldValue.arrayUnion({
          emoji: 'hoho',
          by: 'user1',
        }),
      });
    })
    .then(() => {
      return docRef.get();
    })
    .then((doc) => {
      console.log('#2', doc.data().emojiCounter);

      return docRef.update({
        emojiCounter: firebase.firestore.FieldValue.arrayUnion({
          emoji: 'hihi',
          by: 'user2',
        }),
      });
    })
    .then(() => {
      return docRef.get();
    })
    .then((doc) => {
      console.log('#3', doc.data().emojiCounter);
    });


Note that you may encounter a problem with the solution described above, because, depending on your app functions, it may imply that user1 could change the emoji value of user2. If you want to avoid that, you should adopt another approach. For example allow the user to only update his emoji (e.g. with one emoji doc per user) and have a Cloud Function, triggered when one of those docs is updated, and which updates a central document which contains the array. You should use a Transaction to perform the update.


Also, since you are using one array that contains the emojis of all users, note the maximum size of a field value: 1,048,487 bytes.

这篇关于如何使用Firestore中的对象数组更新字段中的特定对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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