简体   繁体   English

将字段单独添加到 firestore 文档

[英]Add field separately to firestore document

The following code creates a firestore collection and adds data to it:以下代码创建一个 firestore 集合并向其中添加数据:

function saveID(sender_psid,complete){
   let data = new Object();
   data.ID = sender_psid;
   data.TASK = complete;
   data.TIME = new Date();
   db.collection('users').add(data);
}

I want to create another function which adds a field to the document at a different time.我想创建另一个 function,它在不同的时间向文档添加一个字段。 I have the following function but am getting the error "TypeError: collectionRef.update is not a function"我有以下 function 但收到错误“TypeError:collectionRef.update 不是函数”

function saveImage(sender_psid,image) {


  let collectionRef = db.collection('users');


  collectionRef.update({IMG:image}).then(res => {
  console.log(`Document updated at ${res.updateTime}`);
});
}

Build a DocumentReference to the document you want to update, then use the update() method on the DocumentReference to indicate only the fields to be added or changed.为要更新的文档构建DocumentReference ,然后使用 DocumentReference 上的update()方法仅指示要添加或更改的字段。 Pass it an object with only properties that match the fields to add or change.向它传递一个仅包含与要添加或更改的字段匹配的属性的对象。

According tothis documentation, There is a better way根据这个文档,有一个更好的方法

const cityRef = db.collection('cities').doc('BJ');

const res = await cityRef.set({
  capital: true
}, { merge: true });

Flutter Example:颤振示例:

var userDoc = Firestore.instance.collection('users').document('replaceIdHere');
userDoc.updateData({'fieldName':'newValue'});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM