[英]Each then() should return a value or throw Firebase cloud functions
I am writing a cloud function for firebase using javascript but I am stuck, I don't know the exact meaning of error and unable to solve it.. The error states: 27:65 error Each then() should return a value or throw promise/always-return我正在使用 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');
});
});
});
Change this:改变这个:
return admin.messaging().sendToDevice(token_id, payload).then(response => {
console.log('This was the notification Feature');
});
To this:对此:
return admin.messaging().sendToDevice(token_id, payload).then(response => {
console.log('This was the notification Feature');
return null; // add this line
});
The then
callback just needs to return a value. then
回调只需要返回一个值。
However, eslint may then complain about nested then()
in your code, which is also an anti-pattern.但是,eslint 可能会在您的代码中抱怨嵌套
then()
,这也是一种反模式。 Your code should really be structured more like this:你的代码应该更像这样的结构:
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');
});
Note that each then chains off of each other, rather than being nested inside each other.请注意,每个然后相互链接,而不是相互嵌套。
Change this:改变这个:
return admin.messaging().sendToDevice(token_id, payload).then(response => {
console.log('This was the notification Feature');
});
into this:进入这个:
return admin.messaging().sendToDevice(token_id, payload).then(response=>{
console.log('This was the notification Feature');
return true;
},err=>
{
throw err;
});
As the error says when using then
you need to return a value.由于错误使用时,说
then
你需要返回一个值。
That's jslinting telling you that each .then
must include a return value.这是jslinting告诉你,每一个
.then
必须包括返回值。 In other words, avoid promise anti pattern .换句话说,避免promise 反模式。
You might find async
functions easier to wrap your head around.您可能会发现
async
函数更容易理解。 Note that you'll need to run the Node 8 runtime for async support though...请注意,您需要运行 Node 8 运行时以获得异步支持...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.