Firebase 函数查询和更新单个 Firestore 文档
Firebase Function query and update a single Firestore document
我正在尝试使用查询从 Firebase 函数更新用户的 firestore 文档,但在使其正常工作时遇到问题。我的函数代码如下:
const functions = require('firebase-functions');
// The Firebase Admin SDK to access Firestore.
const admin = require('firebase-admin');
admin.initializeApp();
/**
* A webhook handler function for the relevant Stripe events.
*/
// Here would be the function that calls updatePlan and passes it the customer email,
// I've omitted it to simplify the snippet
const updatePlan = async (customerEmail) => {
await admin.firestore()
.collection('users').where('email', '==', customerEmail).get()
.then((doc) => {
const ref = doc.ref;
ref.update({ 'purchasedTemplateOne': true });
});
};
当查询为 运行:
时,我在 firebase 日志中收到以下错误
Exception from a finished function: TypeError: Cannot read properties of undefined (reading 'update')
任何关于我可能做错了什么的帮助或关于如何实现这一目标的建议将不胜感激,提前致谢!
更新:
我能够通过更深入地了解 Firestore 查询来解决我的问题:
const updatePlan = (customerEmail) => {
const customerQuery = admin.firestore().collection("users").where("email", "==", customerEmail)
customerQuery.get().then(querySnapshot => {
if (!querySnapshot.empty) {
// Get just the one customer/user document
const snapshot = querySnapshot.docs[0]
// Reference of customer/user doc
const documentRef = snapshot.ref
documentRef.update({ 'purchasedTemplateOne': true })
functions.logger.log("User Document Updated:", documentRef);
}
else {
functions.logger.log("User Document Does Not Exist");
}
})
};
错误消息告诉您 doc.ref
未定义。对象 doc
.
上没有 属性 ref
这可能是因为您误解了 Firestore 查询生成的对象。即使您期望的是单个文档,过滤查询也可以 return 零个或多个文档。这些文档始终以 QuerySnapshot 类型的对象表示。这就是 doc
实际上是 - 一个 QuerySnapshot - 所以你需要这样对待它。
也许你应该检查 size of the result set before you access the docs array to see what's returned by the query. This is covered in the documentation。
我正在尝试使用查询从 Firebase 函数更新用户的 firestore 文档,但在使其正常工作时遇到问题。我的函数代码如下:
const functions = require('firebase-functions');
// The Firebase Admin SDK to access Firestore.
const admin = require('firebase-admin');
admin.initializeApp();
/**
* A webhook handler function for the relevant Stripe events.
*/
// Here would be the function that calls updatePlan and passes it the customer email,
// I've omitted it to simplify the snippet
const updatePlan = async (customerEmail) => {
await admin.firestore()
.collection('users').where('email', '==', customerEmail).get()
.then((doc) => {
const ref = doc.ref;
ref.update({ 'purchasedTemplateOne': true });
});
};
当查询为 运行:
时,我在 firebase 日志中收到以下错误Exception from a finished function: TypeError: Cannot read properties of undefined (reading 'update')
任何关于我可能做错了什么的帮助或关于如何实现这一目标的建议将不胜感激,提前致谢!
更新:
我能够通过更深入地了解 Firestore 查询来解决我的问题:
const updatePlan = (customerEmail) => {
const customerQuery = admin.firestore().collection("users").where("email", "==", customerEmail)
customerQuery.get().then(querySnapshot => {
if (!querySnapshot.empty) {
// Get just the one customer/user document
const snapshot = querySnapshot.docs[0]
// Reference of customer/user doc
const documentRef = snapshot.ref
documentRef.update({ 'purchasedTemplateOne': true })
functions.logger.log("User Document Updated:", documentRef);
}
else {
functions.logger.log("User Document Does Not Exist");
}
})
};
错误消息告诉您 doc.ref
未定义。对象 doc
.
ref
这可能是因为您误解了 Firestore 查询生成的对象。即使您期望的是单个文档,过滤查询也可以 return 零个或多个文档。这些文档始终以 QuerySnapshot 类型的对象表示。这就是 doc
实际上是 - 一个 QuerySnapshot - 所以你需要这样对待它。
也许你应该检查 size of the result set before you access the docs array to see what's returned by the query. This is covered in the documentation。