Javascript OnCreate函数不触发,但OnWrite函数触发
我有一个oncreate函数,当我在实时数据库中创建数据时,它似乎不会启动。我知道在实时数据库中创建新数据时使用onCreate。请参阅下面的代码 我做错了什么Javascript OnCreate函数不触发,但OnWrite函数触发,javascript,firebase,firebase-realtime-database,google-cloud-functions,Javascript,Firebase,Firebase Realtime Database,Google Cloud Functions,我有一个oncreate函数,当我在实时数据库中创建数据时,它似乎不会启动。我知道在实时数据库中创建新数据时使用onCreate。请参阅下面的代码 我做错了什么 exports.getNewReport = functions.database.ref('/Hotel_Complaints/Users/{usersId}/') .onCreate((snapshot, context) => { // Grab the current value of what was writte
exports.getNewReport =
functions.database.ref('/Hotel_Complaints/Users/{usersId}/')
.onCreate((snapshot, context) => {
// Grab the current value of what was written to the Realtime Database.
var user_id = context.params.usersId;
console.log(user_id);
// Grab the current value of what was written to the Realtime Database.
var eventSnapshot = snapshot.val();
var device_token = admin.database().ref('/Hotel_Staff/'+user_id+'/device_token').once('value');
return device_token.then(result => {
var token_id = result.val();
console.log(token_id);
var str = eventSnapshot.issue_description;
var payload = {
notification: {
title: "New complaint",
body: "New complaint for your department",
}
};
// Send a message to devices subscribed to the provided topic.
return admin.messaging().sendToDevice(token_id, payload).then(function (response) {
// See the MessagingTopicResponse reference documentation for the
// contents of response.
console.log("Successfully sent message:", response);
return;
})
.catch(function (error) {
console.log("Error sending message:", error);
});
});
});
通过稍微调整您的承诺链接,它应该可以做到这一点,请参见以下内容:
exports.getNewReport = functions.database.ref('/Hotel_Complaints/Users/{usersId}/')
.onCreate((snapshot, context) => {
// Grab the current value of what was written to the Realtime Database.
var user_id = context.params.usersId;
console.log(user_id);
// Grab the current value of what was written to the Realtime Database.
var eventSnapshot = snapshot.val();
var device_token = admin.database().ref('/Hotel_Staff/' + user_id + '/device_token').once('value');
return device_token
.then(result => {
var token_id = result.val();
console.log(token_id);
var str = eventSnapshot.issue_description;
var payload = {
notification: {
title: "New complaint",
body: "New complaint for your department"
}
};
// Send a message to devices subscribed to the provided topic.
return admin.messaging().sendToDevice(token_id, payload);
})
.then(response => {
// See the MessagingTopicResponse reference documentation for the
// contents of response.
console.log("Successfully sent message:", response);
return null;
})
.catch(error => {
console.log("Error sending message:", error);
return null
});
});
尝试删除ref中的最后一个/从控制台得到的响应是解析错误。有什么错误吗?我现在已经修复了错误,但仍然没有收到函数启动时的通知。我在帖子中添加了一张图片。您添加的图片显示了一个关于云功能部署的日志项,而不是它的执行。您在日志中看到其他项目了吗?例如,您是否看到console.loguser_id;的结果;?您能否在/Hotel_Complaints/Users/{usersId}/?下显示您的数据库与新用户的图像?您是否查看Firebase控制台上的云功能日志?是否确实在/Hotel_Complaints/Users/{usersId}/下创建了新用户?