[英]Get data from firestore document and use in cloud function
In the user's collection, each user has a document with a customer_id.在用户集合中,每个用户都有一个带有 customer_id 的文档。 I would like to retrieve this customer_id and use it to create a setup intent.
我想检索此 customer_id 并使用它来创建设置意图。
The following code has worked for me in the past.以下代码过去对我有用。 However, all of a sudden it throws the error:
然而,突然间它抛出错误:
Object is possibly 'undefined'
Object 可能是“未定义的”
The error is on the following line under snapshot.data()
in this line:该行中的
snapshot.data()
下的错误位于以下行:
const customerId = snapshot.data().customer_id;
Here is the entire code snippet:这是整个代码片段:
exports.createSetupIntent = functions.https.onCall(async (data, context) => {
const userId = data.userId;
const snapshot = await db
.collection("development")
.doc("development")
.collection("users")
.doc(userId).get();
const customerId = snapshot.data().customer_id;
const setupIntent = await stripe.setupIntents.create({
customer: customerId,
});
const clientSecret = setupIntent.client_secret;
const intentId = setupIntent.id;
return {
clientsecret: clientSecret,
intentId: intentId,
};
});
Any help is appreciated:)任何帮助表示赞赏:)
this is because snapshot.data() may return undefined这是因为 snapshot.data() 可能返回 undefined
there are 2 ways to solve this有两种方法可以解决这个问题
first is assert as non-null, if you have high confident that the data exist首先是断言为非空,如果您对数据存在高度确信
const customerId = snapshot.data()!.customer_id;
second if check for undefined第二个如果检查未定义
const customerId = snapshot.data()?.customer_id;
if(customerId){
// ....
}
I recommend the 2nd method, it is safer我推荐第二种方法,它更安全
I can see you are using a sub collection order,You need to loop through the snapshot data using the forEach loop.我可以看到您正在使用子集合顺序,您需要使用 forEach 循环遍历快照数据。
const customerId = snapshot.data()
customerId.forEach((id)=> {
console.log(id.customer_id)
});
Try this out but.试试这个但是。
The document you're trying to load may not exist, in which case calling data()
on the snapshot will return null
, and thus this line would give an error:您尝试加载的文档可能不存在,在这种情况下,在快照上调用
data()
将返回null
,因此这一行会给出错误:
const customerId = snapshot.data().customer_id;
The solution is to check whether the document you loaded exists, and only then force to get the data from it:解决方案是检查您加载的文档是否存在,然后才强制从中获取数据:
if (snapshot.exists()) {
const customerId = snapshot.data()!.customer_id;
...
}
if you want to fetch user data from docId then you can use something like this:如果你想从 docId 获取用户数据,那么你可以使用这样的东西:
const functions = require("firebase-functions");
var admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
var db = admin.firestore();
db.settings({ timestampsInSnapshots: true });
exports.demoFunction = functions.https.onRequest((request, response) => {
var userId = request.body.userId;
db.collection("user").doc(userId).get().then(snapshot => {
if (snapshot) {
var data = snapshot.data();
// use data to get firestore data
var yourWantedData = data.name;
// use it in your functionality
}
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.