我尝试按照 Firebase 的文档和其他 SO 帖子访问我已成功部署的云函数的参数值。
不幸的是我仍然收到了
类型错误:无法读取未定义的属性“id”
我已经记录了 event.params 并且它输出为未定义,所以我理解这个问题,但不确定从语法上讲我应该如何导出参数值。
下面是我的js代码供参考:
exports.observeCreate = functions.firestore.document('/pathOne/{id}/pathTwo/{anotherId}').onCreate(event => {
console.log(event.params);
//event prints out data but params undefined...
const data = event.data()
var id = event.params.id;
return admin.firestore().collection('path').doc(id).get().then(doc => {
const data = doc.data();
var fcmToken = data.fcmToken;
var message = {
notification: {
title: "x",
body: "x"
},
token: fcmToken
};
admin.messaging().send(message)
.then((response) => {
console.log('Successfully sent message:', response);
return;
})
.catch((error) => {
console.log('Error sending message:', error);
return;
});
return;
})
})
您正在使用 firebase-functions 模块的 1.0 之前的 API,但您安装的实际版本是 1.0 或更高版本。 API 在 1.0 中发生了变化。在此处阅读有关更改的信息 https://firebase.google.com/docs/functions/beta-v1-diff.
Firestore(和其他类型的)触发器现在采用第二个类型参数事件上下文 https://firebase.google.com/docs/reference/functions/functions.EventContext。这有一个属性称为params
包含以前位于 event.params 中的数据。
exports.observeCreate = functions.firestore.document('/pathOne/{id}/pathTwo/{anotherId}').onCreate((snapshot, context) => {
console.log(context.params);
console.log(context.params.id);
});
还请阅读文档 https://firebase.google.com/docs/functions/firestore-events有关 Firestore 触发器的最新信息。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)