从 Firestore 文档中读取字段的最简单方法

我一直在为这个简单的问题绕圈子:我试图读取 Firestore 文档中的一个字段。我正在寻找最直接的解决方案,让我可以使用常量。


const userId = context.params.userId;


const customer = admin.firestore()

        .collection('stripe_customers')

        .doc(userId)

        .collection('customer_info')

        .data('customer_id');

云函数日志给了我这个:


类型错误:admin.firestore(...).collection(...).doc(...).collection(...).data 不是函数


同样的错误


.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);

使用此选项,控制台会记录一个挂起的承诺。不知道如何处理。


我经历了比我想象的更多的尝试并且已经用尽了文档。如果有人知道如何以直接的方式做到这一点,请告诉我。


慕标5832272
浏览 200回答 1
1回答

汪汪一只猫

我一直在为这个简单的问题绕圈子:我试图读取 Firestore 文档中的一个字段。我正在寻找最直接的解决方案,让我可以使用常量。const userId = context.params.userId;const customer = admin.firestore()        .collection('stripe_customers')        .doc(userId)        .collection('customer_info')        .data('customer_id');云函数日志给了我这个:类型错误:admin.firestore(...).collection(...).doc(...).collection(...).data 不是函数同样的错误.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);使用此选项,控制台会记录一个挂起的承诺。不知道如何处理。我经历了比我想象的更多的尝试并且已经用尽了文档。如果有人知道如何以直接的方式做到这一点,请告诉我。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript