Node.js Firebase云函数表示不可访问,每个then()都应返回一个值或抛出promise/always return Firebase云函数的Javascript代码,给出了为推送通知编写的错误 index.js
当运行firebase部署命令时,出现如下错误。如果有人能支持我,我将不胜感激 错误 error Each then()应返回值或抛出promise/always return 错误不可访问代码不可访问 error Each then()应返回值或抛出promise/always returnNode.js Firebase云函数表示不可访问,每个then()都应返回一个值或抛出promise/always return Firebase云函数的Javascript代码,给出了为推送通知编写的错误 index.js,node.js,firebase,firebase-cloud-messaging,Node.js,Firebase,Firebase Cloud Messaging,当运行firebase部署命令时,出现如下错误。如果有人能支持我,我将不胜感激 错误 error Each then()应返回值或抛出promise/always return 错误不可访问代码不可访问 error Each then()应返回值或抛出promise/always return npm ERR! code ELIFECYCLE npm ERR! errno 1 npm ERR! functions@ lint: `eslint .` npm ERR! Exit status 1
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! functions@ lint: `eslint .`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the functions@ lint script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
错误是说您应该从传递给任何then()方法的回调返回一个值。代码中有两个then()方法。如果您没有要发送到承诺链中的下一项工作的内容,只需在函数末尾返回null即可
不过,代码中还有一个更大的问题。在函数的顶层有两个
return
语句,一个接一个。这几乎肯定会导致另一个错误,或者至少无法达到您的预期。(您必须只返回一个承诺,在所有工作完成后解决。)道格感谢您的评论。然后我改变了密码。现在它已部署完毕,没有任何错误。但调用该方法后,firebase函数控制台中会出现错误。(调用该方法意味着使用android应用程序添加新通知)
在有效负载中,没有令牌:deviceToken部分也不工作
'use strict'
//Install functions and admin sdks'
const functions = require('firebase-functions');
const admin =require('firebase-admin');
var FCM = require('fcm-push');
admin.initializeApp();
var serverKey = 'MY_SERVER_KEY_HERE';
var fcm = new FCM(serverKey);
exports.sendNotification = functions.database.ref('/notifications/{user_id}/{notification_id}').onWrite((change, context) => {
//onwrite will run if data changed
const user_id = context.params.user_id;
const notification = context.params.notification;
console.log('user : '+user_id)
console.log('notification : '+notification)
// If exit the function.
if (!change.after.val()) {
return console.log('User ', user_id, 'notification', notification);
}
// Only edit data when it is first created.
if (change.before.exists()) {
return null;
}
// Exit when the data is deleted.
if (!change.after.exists()) {
return null;
}
const deviceToken = admin.database().ref(`/Users/${user_id}/device_token`).once('value');
console.log("Dev TOken : "+deviceToken);
return admin.database().ref(`/Users/${user_id}/device_token`).once('value').then(result => {
const payload = {
notification:{
title:"Friend Request",
body:"You have recieved a new friend request",
},
tokens:deviceToken
};
return admin.messaging().send(payload).then(response =>{
return console.log('This is the notify feature');
}).catch(err => {
return console.log('Error getting documents', err);
});
})
});
输出:-
如果有错误,有人能纠正代码吗?我希望它能正常工作,您应该像这样返回
return console.log('This is the notify feature');
谢谢大家。我修好了
'use strict'
//Install functions and admin sdks'
const functions = require('firebase-functions');
const admin =require('firebase-admin');
admin.initializeApp();
exports.sendNotification = functions.database.ref('/notifications/{user_id}/{notification_id}').onWrite((change, context) => {
//onwrite will run if data changed
const user_id = context.params.user_id;
//const from = context.params.from;
const notification_id = context.params.notification_id;
console.log('user : '+user_id);
console.log('notification_id : '+notification_id);
//console.log('notification : '+notification)
//console.log('from : '+from)
// If exit the function.
if (!change.after.val()) {
return console.log('Sender ', user_id, 'From', from);
}
// Only edit data when it is first created.
if (change.before.exists()) {
return null;
}
// Exit when the data is deleted.
if (!change.after.exists()) {
return null;
}
// if (context.params === null) {
// return console.log("Notification Has Been Deleted");
// }
// let token_id = null;
const getDeviceTokensPromise = admin.database().ref(`/Users/${user_id}/device_token`).once('value');
return getDeviceTokensPromise.then(result => {
const payload = {
notification: {
title: 'You have a new follower!',
body: `is now following you.`,
icon: 'default'
}
};
return admin.messaging().sendToDevice(result.val(), payload);
}).then(res => {
return console.log(JSON.stringify(res));
});
});
对于云函数,如果其https触发器,则必须向用户返回响应;如果任何其他类型的触发器,则必须返回承诺。只是
返回承诺。解决(“完成”)代码>我已尝试返回null,但对我无效
'use strict'
//Install functions and admin sdks'
const functions = require('firebase-functions');
const admin =require('firebase-admin');
admin.initializeApp();
exports.sendNotification = functions.database.ref('/notifications/{user_id}/{notification_id}').onWrite((change, context) => {
//onwrite will run if data changed
const user_id = context.params.user_id;
//const from = context.params.from;
const notification_id = context.params.notification_id;
console.log('user : '+user_id);
console.log('notification_id : '+notification_id);
//console.log('notification : '+notification)
//console.log('from : '+from)
// If exit the function.
if (!change.after.val()) {
return console.log('Sender ', user_id, 'From', from);
}
// Only edit data when it is first created.
if (change.before.exists()) {
return null;
}
// Exit when the data is deleted.
if (!change.after.exists()) {
return null;
}
// if (context.params === null) {
// return console.log("Notification Has Been Deleted");
// }
// let token_id = null;
const getDeviceTokensPromise = admin.database().ref(`/Users/${user_id}/device_token`).once('value');
return getDeviceTokensPromise.then(result => {
const payload = {
notification: {
title: 'You have a new follower!',
body: `is now following you.`,
icon: 'default'
}
};
return admin.messaging().sendToDevice(result.val(), payload);
}).then(res => {
return console.log(JSON.stringify(res));
});
});