簡體   English   中英

如何從雲功能中的 firestore 獲取文檔

[英]How to get a document from firestore in cloud functions

我嘗試了很多方法,例如

const admin = require("firebase-admin");
admin.initializeApp();

const db = admin.firestore();

const docRef = db.collection("users").doc(dynamicDocID).get()
const docRef = db.collection("users").doc(dynamicDocID)



以及許多其他問題,並不斷得到 undefined 或似乎永遠無法解決的 promise

如果有的話,似乎找不到合適的文檔

由於 Firebase 的 Cloud Functions 是在 Node.js 中編寫的,請查看Firestore 文檔中的 Node.js 示例。

基於此:

const docRef = db.collection("users").doc(dynamicDocID)
const document = await docRef.get()
console.log(document.id, document.data())

或者,如果您不能使用await

const docRef = db.collection("users").doc(dynamicDocID)
return docRef.get().then((document) => {
  console.log(document.id, document.data())
})

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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