'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命令时,会出现如下错误。如果有人能支持,我会很感激。
错误每个then()应返回一个值或抛出promise / always-return 错误无法访问的代码无法访问 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.
答案 0 :(得分:5)
我希望它能正常工作,你应该像这样回归
return console.log('This is the notify feature');
答案 1 :(得分:2)
错误是说你应该从传递给任何then()方法的回调中返回一个值。您的代码中有两个then()方法。如果您没有任何内容可以发送到promise链中的下一个工作项,只需在函数末尾return null
。
但是,您的代码中存在更大的问题。您在函数的顶层有两个return
语句,一个在另一个之后。这几乎肯定会导致另一个错误,或者至少不能达到预期的效果。 (您必须只返回一个在所有工作完成后解决的承诺。)
答案 2 :(得分:0)
'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);
});
})
});
输出: - https://i.imgur.com/9PRwMbi.png
如果有任何错误,任何人都可以更正代码吗?
答案 3 :(得分:0)
全部谢谢。我解决了这个问题。
'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));
});
});
答案 4 :(得分:-1)
对于云功能,如果其https触发器,则它必须向用户返回响应,如果还有任何其他类型的触发器,则它必须返回Promise。只是return Promise.resolve("Done");
我曾尝试返回null,但这对我不起作用。