首页 > 解决方案 > 无法在 firestore 中使用 set() 合并数据,而是覆盖数据

问题描述

我想在 Friends 集合中附加 UserId 文档,因为他们接受新的好友请求,但uid文档中的字段被最近接受的好友请求覆盖。merge: true 尽管使用了我期望发生的事情,但我收到了这个错误:

Friends (collection)
    userId (document) 
        uid: randomValue, 
        uid: randomValue,
        .
        .and so on

实际发生了什么:

最初接受的好友请求

接受其他用户的好友请求后

我的代码:

const uid = auth().currentUser.uid;
  function onAcceptRequest() {
    firestore()
      .collection('Friends')
      .doc(`${uid}`)
      .set(
        {
          uid: senderId,
        },
        {merge: true},
      )
      .then(() => {
        firestore()
          .collection('Friends')
          .doc(`${senderId}`)
          .set(
            {
              uid: uid,
            },
            {merge: true},
          )
          .then(console.log('Befriended'))
          .then(() => deleteRequest())
          .catch(err => console.log('senders err', err));
      })
      .catch(err => console.log('currentUsers err', err));
  }

如果有任何其他方法可以完成同样的事情,那么请提出建议。谢谢你。

标签: javascriptfirebasereact-nativegoogle-cloud-firestore

解决方案


推荐阅读