[英]Easiest way to read field from Firestore document
I have been running in circles over this simple problem: I am trying to read a field in a Firestore document.我一直在围绕这个简单的问题转圈:我正在尝试读取 Firestore 文档中的一个字段。 I am looking for the most straightforward solution that gives me a const to work with.
我正在寻找最直接的解决方案,让我可以使用 const。
const userId = context.params.userId;
const customer = admin.firestore()
.collection('stripe_customers')
.doc(userId)
.collection('customer_info')
.data('customer_id');
The cloud function log gives me this:云 function 日志给了我这个:
TypeError: admin.firestore(...).collection(...).doc(...).collection(...).data is not a function
TypeError: admin.firestore(...).collection(...).doc(...).collection(...).data 不是 function
Same error with同样的错误
.data().customer_id;
Here is another option I tried:这是我尝试的另一个选项:
const customerDoc = admin.firestore()
.collection('stripe_customers')
.doc(userId)
.collection('customer_info')
.doc('customer_object');
const customer = customerDoc.get('customer_id');
console.log(customer);
With this option, the console logs a pending promise.使用此选项,控制台会记录待处理的 promise。 Not sure how to work with that.
不知道如何使用它。
I've been through more tries than I can count and have exhausted the documentation.我经历了比我数不清的更多尝试,并且已经用尽了文档。 If anyone knows how to do this in a straight forward way, please let me know.
如果有人知道如何以直接的方式做到这一点,请告诉我。
Ok, the almost correct answer was the second one.好的,几乎正确的答案是第二个。 I just had to await the promise.
我只需要等待 promise。 Here is the code for anyone who is interested:
这是任何有兴趣的人的代码:
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.