【问题标题】:Firestore UPDATE a single field but creates another nodeFirestore 更新单个字段但创建另一个节点
【发布时间】:2018-06-07 02:45:24
【问题描述】:

我正在更新单个字段名称,即“名称”,我成功编辑它,但它创建了另一个节点。像这样

这是我的代码:

const user = firebase.auth().currentUser;
    let uid;
    if (user != null) {
      uid = user.uid;
      const db = firebase.firestore();
      const docRef = db.collection('users').doc(uid);
      docRef.update({
        name,
        timestamp: firebase.firestore.FieldValue.serverTimestamp()
      }).then(() => {
        console.log('Profile Successfully Edited!');
      }).catch((error) => {
        console.log('Error updating the document:', error);
      })
    }

【问题讨论】:

  • 名字是什么?更新中
  • 是我要更新的字段名。

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


【解决方案1】:

我已经找到解决办法了,

if (user != null) {
      uid = user.uid;
      const db = firebase.firestore();
      db.collection('users').doc(uid).update({
        name })....

我只是直接将 .update 放到 db 引用中。

【讨论】:

    【解决方案2】:

    试试

    docRef.update({
        name:"Christian",
        timestamp: firebase.firestore.FieldValue.serverTimestamp()
    })
    

    你可以在name:之后传递一个字符串var

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-05-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-15
      • 1970-01-01
      • 2021-12-31
      • 1970-01-01
      相关资源
      最近更新 更多