Firebase 云函数的 Javascript 代码给出了为推送通知编写的错误
索引.js
'use strict'
//Install functions and admin sdks'
const functions = require('firebase-functions');
const admin =require('firebase-admin');
admin.initializeApp(functions.config().firebase);
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;
if (context.params === null) {
return console.log("Notification Has Been Deleted");
}
let token_id = null;
const deviceToken = admin.database.ref(`/Users/${user_id}/device_token`).once('value');
return deviceToken.then(result => {
token_id = result.val();
const payload = {
notification:{
title:"Friend Request",
body:"You have recieved a new friend request",
icon:"default"
}
};
});
return admin.messaging().sendToDevice(token_id, payload).then(response =>{
console.log('This is the notify feature');
}).catch(err => {
console.log('Error getting documents', err);
});
});
运行 firebase deploy 命令时出现如下错误。如果有人可以支持,我将不胜感激。
错误
error 每个 then() 应该返回一个值或抛出 promise/always-return error 无法访问的代码 no-unreachable error 每个 then() 应该返回一个值或抛出 promise/always-return
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.
最佳答案
希望一切顺利,你应该这样回来
return console.log('This is the notify feature');
关于node.js - Firebase 云函数说 Unreachable 并且每个 then() 应该返回一个值或抛出 promise/always-return,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49841484/