简体   繁体   English

Firestore expressjs:如何获取特定字段存在的文档并更新文档

[英]Firestore expressjs: how to get document where specific field exists and update the document

So I am using expressjs in the firebase cloud function trying to get a document where a specific field is the same as a value that I already have, So here is the piece of code:所以我在 firebase 云 function 中使用 expressjs 试图获取一个特定字段与我已经拥有的值相同的文档,所以这是一段代码:

const email =data.email (this is coming from stripe webhook ) 
  const document = db
        .collection('ConnectedAccounts')
        .where(`email`, '==', email)
        .get();
      console.log('document', document.data());

after getting this document I want to store a new field to it, I have tried the above code but failed, can anyone help?获得此文档后,我想在其中存储一个新字段,我尝试了上面的代码但失败了,有人可以帮忙吗?

When you execute a query the response is a QuerySnapshot that can contain any number of documents.当您执行查询时,响应是一个可以包含任意数量文档的QuerySnapshot So you need to loop over the documents in that snapshot to get at the individual results:因此,您需要遍历该快照中的文档以获取各个结果:

const email =data.email (this is coming from stripe webhook ) 
const querySnapshot = await db
      .collection('ConnectedAccounts')
      .where(`email`, '==', email)
      .get();
querySnapshot.docs.forEach((docSnapshot) => {
  docSnapshot.ref.update({ ... your update here ...});
})

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

相关问题 如何更新 Firestore 文档中特定字段的值? - How to update value of a specific field in Firestore document? 如何通过文档 ID 从 firestore 网站获取特定字段数据 - How get specific Field data by document id from firestore website 存在 object 键的 Firestore 查询文档 - Firestore query document where object key exists 在 Cloud Firestore 事务中,如果我们不知道文档名称,如何检查文档是否存在于特定集合中 - in Cloud Firestore transaction how Check to see if a Document exists in a specific Collection if we don't know Document name 如何删除 Firestore 文档中的字段? - How to delete a field in a Firestore Document? 如何从文档中获取特定字段值 - How to get specific field value from document 更新 Firestore 集合中最后一个文档中的一个字段 Flutter - Update a field in the last document in Firestore collection Flutter 如何使用帮助文档 ID 更新 Cloud firestore 文档字段? - How can i Update Cloud firestore document Field with the help DocumentID? Flutter:如何获取 Firestore 文档中字段(列表)的长度? - Flutter : How to get the length of field (list) in Firestore document? Firestore:获取在何处找到的文档的子集 - Firestore: Get subcollection of document found with where
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM