[英]Problem getting value of Firestore document in Cloud Functions
我正在编写 Firebase 云 function 以在用户集合中的文档发生更改时从 Algolia 生成受限的 API 密钥。
Algolia 部分工作正常,但我没有从相关的 Firestore 文档中获取用户 ID 值。
当我运行此代码时,第一个 console.log 会引发以下错误: TypeError: change.data is not a function 。
我检查了 Firestore 文档字段名称和路径。
const functions = require("firebase-functions");
const admin = require('firebase-admin');
admin.initializeApp();
//Generate a new API key when a user document is changed. (This will later be changed to trigger when a user document is created in Firestore)
exports.generateAlgoliaAPIKey = functions.firestore
.document('users/{user}')
.onWrite((change, context) => {
const userid = change.data().userid
console.log(userid)
const publicKey = client.generateSecuredApiKey(
'XXXXXXXXXXXXXXXX', // A search key that you keep private
{
filters: `owninguser:${userid}`
}
);
console.log("this is the new key: " + publicKey)
});
您可以使用 change.after.data() 访问新数据或使用change.before.data()
change.after.data()
访问以前的数据。
在此处查看触发函数的文档。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.