【问题标题】:add objects to array without overwriting将对象添加到数组而不覆盖
【发布时间】:2022-01-20 14:28:39
【问题描述】:

我有我当前的代码

let array = {};
          array[date] = [
            {
              Name: Name,
              Cell: Cell,
              Age: Age,
              userId: userId,
              Time: time,
              Type: type,
              read: false,
            },
          ];

像这样上传到firebase

但每次我为同一日期添加新条目时,它都会覆盖现有数据。我希望将数据添加到地图“1”等,而不是覆盖地图“0”中的数据

更新:我已尝试以下操作,但收到错误消息。不知道我这样做是否正确,我还在学习。

 let array = {};
          array[date] = [];
          try {
            await firebase
              .firestore()
              .collection("Notifications")
              .doc(`${helper}`)
              .update(
                {
                  array: firebase.firestore.FieldValue.arrayUnion({
                    Name: Name,
                    Cell: Cell,
                    Age: Age,
                    userId: userId,
                    Time: time,
                    Type: type,
                    read: false,
                  }),
                },

                { merge: true }
              );
          } catch (e) {
            alert(e);
            console.log(e);
          }

通过推送更新:

 // at beginning of script
          let array = {};

          // later, when you want to add to it
          if (!array[date]) {
            array[date] = [];
          }
          array[date].push({
            Name: Name,
            Cell: Cell,
            Age: Age,
            userId: userId,
            Time: time,
            Type: type,
            read: false,
          });

    

          try {
            await firebase
              .firestore()
              .collection("Notifications")
              .doc(`${helper}`)
              .set(
                array,

                { merge: true }
              );
          } catch (e) {
            alert(e);
            console.log(e);
          }
        },

解决方案:

try {
  await firebase
    .firestore()
    .collection("Notifications")
    .doc(`${helper}`)
    .update({
      [date]: firebase.firestore.FieldValue.arrayUnion({
        Name: Name,
        Cell: Cell,
        Age: Age,
        userId: userId,
        Time: time,
        Type: type,
        read: false,
      }),
    });
} catch (e) {
  alert(e);
  console.log(e);
}

【问题讨论】:

  • 你能分享你的代码没有按预期工作吗?
  • 这是一个对象,而不是一个数组。
  • @Dharmaraj 我已经更新了问题
  • 去掉{merge: true}...只有在使用set()时才能使用
  • 使用第一个,但要么将update 更改为set,要么从更新中删除{merge: true}

标签: javascript firebase react-native google-cloud-firestore


【解决方案1】:

由于您设置了整个 2022-01-03 字段,因此您将覆盖该字段中的所有现有值。

如果您想将您指定的新值/对象与字段use the array-union operator 中的现有值合并。

【讨论】:

  • 感谢您的回复,我已经用我的尝试和您的解决方案更新了原始问题。我收到一个错误
  • 一旦我想通了,数组联合就成功了,谢谢!
【解决方案2】:

使用push() 添加到数组中。

// at beginning of script
let array = {};

// later, when you want to add to it
if (!array[date]) {
  array[date] = [];
}
array[date].push({
  Name: Name,
  Cell: Cell,
  Age: Age,
  userId: userId,
  Time: time,
  Type: type,
  read: false,
});

【讨论】:

  • 感谢您的回复,我已经尝试过您的解决方案,但仍然覆盖数据
  • 你应该只在脚本开头有一次let array = {}。其他部分是每次你想添加东西时完成的。
  • 我已经用您的解决方案更新了原始问题,如果您可以查看的话。它仍在覆盖数据。我做对了吗?
  • 我不知道 firebase,所以我无法帮助您解决这部分问题。但是,每当您说遇到错误时,您都需要发布确切的错误消息。
猜你喜欢
  • 2021-07-16
  • 2015-05-08
  • 2018-12-13
  • 2015-06-25
  • 1970-01-01
  • 2021-06-04
  • 1970-01-01
  • 1970-01-01
  • 2011-05-20
相关资源
最近更新 更多