Ikr*_*azi 5 javascript firebase google-cloud-functions
我正在使用javascript为firebase写云函数,但是我被困住了,我不知道错误的确切含义并且无法解决。.错误状态:27:65错误每个then()应该返回一个值或抛出承诺/总是回报
'use strict'
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) => {
const user_id = context.params.user_id;
const notification_id = context.params.notification_id;
console.log('We have a notification from : ', user_id);
if (!change.after.val()) {
return console.log('A Notification has been deleted from the database : ', notification_id);
}
const deviceToken = admin.database().ref(`/ServiceProvider/${user_id}/device_token`).once('value');
return deviceToken.then(result => {
const token_id = result.val();
const payload = {
notification: {
title : "New Friend Request",
body: "You Have Received A new Friend Request",
icon: "default"
}
};
return admin.messaging().sendToDevice(token_id, payload).then(response => {
console.log('This was the notification Feature');
});
});
});
Run Code Online (Sandbox Code Playgroud)
更改此:
return admin.messaging().sendToDevice(token_id, payload).then(response => {
console.log('This was the notification Feature');
});
Run Code Online (Sandbox Code Playgroud)
对此:
return admin.messaging().sendToDevice(token_id, payload).then(response => {
console.log('This was the notification Feature');
return null; // add this line
});
Run Code Online (Sandbox Code Playgroud)
该then回调只需要返回一个值。
但是,eslint可能随后抱怨嵌套then()在您的代码中,这也是一种反模式。您的代码实际上应该更像这样构造:
const deviceToken = admin.database().ref(`/ServiceProvider/${user_id}/device_token`).once('value');
return deviceToken.then(result => {
// redacted stuff...
return admin.messaging().sendToDevice(token_id, payload);
}).then(() => {
console.log('This was the notification Feature');
});
Run Code Online (Sandbox Code Playgroud)
请注意,它们彼此之间是相互链接的,而不是彼此嵌套。