簡體   English   中英

Firestore expressjs:如何獲取特定字段存在的文檔並更新文檔

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

所以我在 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());

獲得此文檔后,我想在其中存儲一個新字段,我嘗試了上面的代碼但失敗了,有人可以幫忙嗎?

當您執行查詢時,響應是一個可以包含任意數量文檔的QuerySnapshot 因此,您需要遍歷該快照中的文檔以獲取各個結果:

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM