Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/369.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/logging/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Firestore更新与合并-如何覆盖文档的一部分_Javascript_Firebase_Google Cloud Firestore - Fatal编程技术网

Javascript Firestore更新与合并-如何覆盖文档的一部分

Javascript Firestore更新与合并-如何覆盖文档的一部分,javascript,firebase,google-cloud-firestore,Javascript,Firebase,Google Cloud Firestore,在Firestore中更新文档时,我希望保留大部分文档,但更改一个包含对象的属性 我拥有的 { name: "John Doe", email: "me@example.com", friends: { a: {...}, b: {...}, c: {...}, d: {...}, e: {...}, f: {...}, } } { name: "John Doe", email: "me@example.co

在Firestore中更新文档时,我希望保留大部分文档,但更改一个包含对象的属性

我拥有的

{
  name: "John Doe",
  email: "me@example.com",
  friends: {
     a: {...},
     b: {...},
     c: {...},
     d: {...},
     e: {...},
     f: {...},
  }
}
{
  name: "John Doe",
  email: "me@example.com",
  friends: {
     x: {...},
     y: {...},
     z: {...},
  }
}
现在,我有了一个新的friends对象,如
{x:…,y:…,z:…}

我想覆盖文档的
朋友
树,但保留所有其他字段

我希望它看起来像什么

{
  name: "John Doe",
  email: "me@example.com",
  friends: {
     a: {...},
     b: {...},
     c: {...},
     d: {...},
     e: {...},
     f: {...},
  }
}
{
  name: "John Doe",
  email: "me@example.com",
  friends: {
     x: {...},
     y: {...},
     z: {...},
  }
}
但是,如果我执行
firestore.doc(…).update({friends:{…},{merge:true})

我目前得到的

{
  name: "John Doe",
  email: "me@example.com",
  friends: {
     a: {...},
     b: {...},
     c: {...},
     d: {...},
     e: {...},
     f: {...},
     x: {...},
     y: {...},
     z: {...},
  }
}
我知道我可以做两次更新,即删除字段然后重新设置,或者我可以阅读文档、更改对象并保存它而不合并

但是,有没有一种聪明的方法可以覆盖对象(映射),同时保持文档的其余部分不变呢?

因为您正在使用该方法,所以只需调用它而不使用合并选项

firestore.doc(...).update({friends: {...}})

请注意,它有两个不同的签名:

update(data: UpdateData)

如果传递给该方法的第一个参数是一个对象,它将考虑使用第一个签名,因此只能传递一个参数。这样做

firestore.doc(...).update({friends: {...}}, { merge: true })
应生成以下错误:

错误:函数DocumentReference.update()需要1个参数,但调用时使用了2个参数


另一方面,你可以这样称呼它:

  firestore
    .collection('...')
    .doc('...')
    .update(
      'friends.c',
      'Tom',
      'email', 
      'mynew_email@example.com',
      'lastUpdate',
      firebase.firestore.FieldValue.serverTimestamp()
    );

最后,为了完整起见,请注意,如果执行以下操作(传递one字符串)

您将得到以下错误

错误:函数DocumentReference.update()至少需要2个参数,但调用时使用了1个参数


这很有意义:-)

谢谢-不知何故,我没有注意到可以更新对象的特定字段,这很有意义,因此如果我将字段路径作为第一个参数,只覆盖文档的该部分。我还认为我对
update
set
方法有点困惑,所以我想,当我需要合并时,我会调用
set(..{merge:true{})
当我需要覆盖特定字段时,我会调用
更新(“friends”,{…})