无法执行 Firestore 功能
Unable to execute Firestore function
我已经在 Firebase 功能上创建了服务器,现在我要在这个功能上创建计划通知。但是这个函数被调用但没有触发或执行。请帮助如何使用节点 JS 执行这个基于时间的功能。
var date = new Date(2018, 6, 17, 10, 50);
exports.newEvent = functions.firestore.document("Events/{event_id}").onWrite((change, context) => {
console.log('event id', context.params.event_id);
// console.log('Join_event id', context.params.event_id);
const id = context.params.event_id;
return admin.firestore().collection("Events").doc(id).get()
.then(queryResult =>{
const uid=queryResult.data().user;
console.log("User id from event",uid);
const da=queryResult.data().date;
console.log("User id from event",da);
const req = context.params.event_id;
schedule.scheduleJob(date, function(){
console.log('running every 2 minute');
console.log(id);
return admin.firestore().collection("Events").doc(id).get()
.then(queryResult =>{
const uid=queryResult.data().user;
console.log("User id from event",uid);
return admin.firestore().collection("User_data").doc(uid).get()
.then(snapshot2 =>
{
const tid3=snapshot2.data().token_id;
const useremail =snapshot2.data().user_email;
const username =snapshot2.data().user_firstname;
console.log(" User Name: ", username);
console.log(" User Email: ", useremail);
console.log(" Token_id: ",tid3);
const payload3 = {
notification:{
title:username,
body:"Event test notifiction",
icon: "default"
}
};
console.log(payload3);
const options4 = {
priority: "high",
timeToLive: 60 * 60 *24,
content_available: true,
};
return admin.messaging().sendToDevice(tid3, payload3,options4).then(result => {
return console.log('Notify when event time changed');
});
});
});
});
return true;
});
});
我已经创建了演示功能。它按时调用并成功打印日志。
谢谢
首先,云函数的执行时间限制为 60 秒,之后函数实例将不会执行,因此您的 cron 作业将不会 运行。
其次,您正在同一节点上的 firestore 功能中使用 admin sdk 从 firestore 读取数据。 change
变量中有数据。在此处查看 firebase 文档:https://firebase.google.com/docs/functions/firestore-events
此外,FCM 不支持延迟交付。
对于您的情况,您需要自己的节点服务器来侦听您需要的路径。
我已经在 Firebase 功能上创建了服务器,现在我要在这个功能上创建计划通知。但是这个函数被调用但没有触发或执行。请帮助如何使用节点 JS 执行这个基于时间的功能。
var date = new Date(2018, 6, 17, 10, 50);
exports.newEvent = functions.firestore.document("Events/{event_id}").onWrite((change, context) => {
console.log('event id', context.params.event_id);
// console.log('Join_event id', context.params.event_id);
const id = context.params.event_id;
return admin.firestore().collection("Events").doc(id).get()
.then(queryResult =>{
const uid=queryResult.data().user;
console.log("User id from event",uid);
const da=queryResult.data().date;
console.log("User id from event",da);
const req = context.params.event_id;
schedule.scheduleJob(date, function(){
console.log('running every 2 minute');
console.log(id);
return admin.firestore().collection("Events").doc(id).get()
.then(queryResult =>{
const uid=queryResult.data().user;
console.log("User id from event",uid);
return admin.firestore().collection("User_data").doc(uid).get()
.then(snapshot2 =>
{
const tid3=snapshot2.data().token_id;
const useremail =snapshot2.data().user_email;
const username =snapshot2.data().user_firstname;
console.log(" User Name: ", username);
console.log(" User Email: ", useremail);
console.log(" Token_id: ",tid3);
const payload3 = {
notification:{
title:username,
body:"Event test notifiction",
icon: "default"
}
};
console.log(payload3);
const options4 = {
priority: "high",
timeToLive: 60 * 60 *24,
content_available: true,
};
return admin.messaging().sendToDevice(tid3, payload3,options4).then(result => {
return console.log('Notify when event time changed');
});
});
});
});
return true;
});
});
我已经创建了演示功能。它按时调用并成功打印日志。
谢谢
首先,云函数的执行时间限制为 60 秒,之后函数实例将不会执行,因此您的 cron 作业将不会 运行。
其次,您正在同一节点上的 firestore 功能中使用 admin sdk 从 firestore 读取数据。 change
变量中有数据。在此处查看 firebase 文档:https://firebase.google.com/docs/functions/firestore-events
此外,FCM 不支持延迟交付。
对于您的情况,您需要自己的节点服务器来侦听您需要的路径。