![](/img/trans.png)
[英]How to read array document field from firebase firestore (React js)
[英]Easiest way to read field from Firestore document
我一直在圍繞這個簡單的問題轉圈:我正在嘗試讀取 Firestore 文檔中的一個字段。 我正在尋找最直接的解決方案,讓我可以使用 const。
const userId = context.params.userId;
const customer = admin.firestore()
.collection('stripe_customers')
.doc(userId)
.collection('customer_info')
.data('customer_id');
雲 function 日志給了我這個:
TypeError: admin.firestore(...).collection(...).doc(...).collection(...).data 不是 function
同樣的錯誤
.data().customer_id;
這是我嘗試的另一個選項:
const customerDoc = admin.firestore()
.collection('stripe_customers')
.doc(userId)
.collection('customer_info')
.doc('customer_object');
const customer = customerDoc.get('customer_id');
console.log(customer);
使用此選項,控制台會記錄待處理的 promise。 不知道如何使用它。
我經歷了比我數不清的更多嘗試,並且已經用盡了文檔。 如果有人知道如何以直接的方式做到這一點,請告訴我。
好的,幾乎正確的答案是第二個。 我只需要等待 promise。 這是任何有興趣的人的代碼:
let customerRef = admin.firestore()
.collection('stripe_customers')
.doc(userId)
.collection('customer_info')
.doc('customer_object');
const customer = await customerRef.get()
.then(doc => {
return doc.data().customer_id;
})
.catch(err => {
console.log('Error getting document', err);
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.