更新数组中的对象-Firebase函数

更新数组中的对象-Firebase函数,firebase,google-cloud-firestore,Firebase,Google Cloud Firestore,函数在集合日志中写入时执行,它检查集合总计中是否存在文档。如果文档存在,则它正试图在数组的[0]处用编号+1更新编号对象 这是我的密码: ...//some code var washingtonRef = admin.firestore().collection('totals').doc(entryDate_show); washingtonRef.get().then((doc: any)=> { if (doc.exists) {

函数在集合
日志中写入时执行,它检查集合
总计中是否存在文档。如果文档存在,则它正试图在数组的[0]处用
编号+1
更新
编号
对象

这是我的密码:

...//some code
var washingtonRef = admin.firestore().collection('totals').doc(entryDate_show);
        washingtonRef.get().then((doc: any)=> {
          if (doc.exists) {
            console.log("doc found");
  console.log("Document data:", doc.data());
  washingtonRef.update({
    [auth_ID]: admin.firestore.FieldValue.arrayUnion(
      { number: doc.data().number+1,  // here it is trying to do +1
        fullname: fullname, 
        authid: auth_ID },
      )
  });
...//some code
问题:它没有按预期工作

在数组中,它正在添加编号为NaN的新对象
[1]

预期行为:数量:2[0]

控制台的附件图片:


FieldValue.arrayUnion()
向数组字段添加新元素。这就是你看到一个新物体的原因


Firestore不提供使用数组项的索引对其进行修改的更新操作。如果要按索引更新数组中的项,则必须读取文档,在内存中修改数组,然后将数组字段更新回文档

如果可能的话,你能给我举个例子吗。我知道这里的数组名,如果它删除旧的数组并用我期望的数字+1覆盖也可以。我认为文档对和相当清楚。您只需在执行更新之前修改内存中的数组即可。无法获取该数组的数值,请按以下方式尝试
doc.data().auth\u ID[0]。数值但在函数日志中我看到错误
doc.data().auth_ID[0]。编号