gpt4 book ai didi

javascript - 每个 then() 都应该返回一个值或抛出 Firebase 云函数

转载 作者:行者123 更新时间:2023-12-04 02:17:49 26 4
gpt4 key购买 nike

我正在使用javascript为firebase编写一个云函数,但我被卡住了,我不知道错误的确切含义并且无法解决它..
错误状态:27:65 错误每个 then() 应该返回一个值或抛出 promise/always-return

'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');

});

});

});

最佳答案

改变这个:

    return admin.messaging().sendToDevice(token_id, payload).then(response => {

console.log('This was the notification Feature');

});

对此:
    return admin.messaging().sendToDevice(token_id, payload).then(response => {

console.log('This was the notification Feature');
return null; // add this line

});
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');
});

请注意,每个然后相互链接,而不是相互嵌套。

关于javascript - 每个 then() 都应该返回一个值或抛出 Firebase 云函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53354417/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com