每当将数据添加到Firestore中的某个集合时,我都尝试使用Firebase Cloud Functions向所有用户发送通知。
这是我的云功能代码:
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config.firebase);
exports.makeUppercase = functions.firestore.document('messages/{messageId}')
.onCreate((snap, context) => {
const value = snap.data().original;
console.log('notifying ' + value);
return Promise.all([value]).then(result => {
const value = result[0].data().value;
const payload = {
notification: {
title: "Added",
body: "Data Added"
}
};
admin.messaging().send(payload, false).then(result => {
console.log("Notification sent!");
});
});
});
当我尝试使用firebase deploy --only functions
创建此功能时,控制台出现错误。
/Users/rachitgoyal/functions/index.js
35:40 error Each then() should return a value or throw promise/always-return
✖ 1 problem (1 error, 0 warnings)
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.
npm ERR! A complete log of this run can be found in:
npm ERR! /Users/rachitgoyal/.npm/_logs/2019-05-08T08_36_48_838Z-debug.log
Error: functions predeploy error: Command terminated with non-zero exit code1
debug.log中的其他日志以供参考:
0 info it worked if it ends with ok
1 verbose cli [ '/usr/local/bin/node',
1 verbose cli '/usr/local/bin/npm',
1 verbose cli '--prefix',
1 verbose cli '/Users/rachitgoyal/functions',
1 verbose cli 'run',
1 verbose cli 'lint' ]
2 info using npm@6.4.1
3 info using node@v10.15.3
4 verbose run-script [ 'prelint', 'lint', 'postlint' ]
5 info lifecycle functions@~prelint: functions@
6 info lifecycle functions@~lint: functions@
7 verbose lifecycle functions@~lint: unsafe-perm in lifecycle true
8 verbose lifecycle functions@~lint: PATH: /usr/local/lib/node_modules/npm/node_modules/npm-lifecycle/node-gyp-bin:/Users/rachitgoyal/functions/node_modules/.bin:/Users/rachitgoyal/.npm-global/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin
9 verbose lifecycle functions@~lint: CWD: /Users/rachitgoyal/functions
10 silly lifecycle functions@~lint: Args: [ '-c', 'eslint .' ]
11 silly lifecycle functions@~lint: Returned: code: 1 signal: null
12 info lifecycle functions@~lint: Failed to exec lint script
13 verbose stack Error: functions@ lint: `eslint .`
13 verbose stack Exit status 1
13 verbose stack at EventEmitter.<anonymous> (/usr/local/lib/node_modules/npm/node_modules/npm-lifecycle/index.js:301:16)
13 verbose stack at EventEmitter.emit (events.js:189:13)
13 verbose stack at ChildProcess.<anonymous> (/usr/local/lib/node_modules/npm/node_modules/npm-lifecycle/lib/spawn.js:55:14)
13 verbose stack at ChildProcess.emit (events.js:189:13)
13 verbose stack at maybeClose (internal/child_process.js:970:16)
13 verbose stack at Process.ChildProcess._handle.onexit (internal/child_process.js:259:5)
14 verbose pkgid functions@
15 verbose cwd /Users/rachitgoyal
16 verbose Darwin 18.5.0
17 verbose argv "/usr/local/bin/node" "/usr/local/bin/npm" "--prefix" "/Users/rachitgoyal/functions" "run" "lint"
18 verbose node v10.15.3
19 verbose npm v6.4.1
20 error code ELIFECYCLE
21 error errno 1
22 error functions@ lint: `eslint .`
22 error Exit status 1
23 error Failed at the functions@ lint script.
23 error This is probably not a problem with npm. There is likely additional logging output above.
24 verbose exit [ 1, true ]
这里有趣的是,如果我在index.js中注释以下代码,则部署可以正常工作。
const payload = {
notification: {
title: "Added",
body: "Data Added"
}
};
admin.messaging().send(payload, false).then(result => {
console.log("Notification sent!");
});
所以我假设我在初始化通知时做错了什么。或我没有将值正确返回给Promise。任何帮助,我们将不胜感激。
答案 0 :(得分:1)
Promise.all()
和admin.messaging().send()
都返回一个Promise,因此您需要链接这些诺言。
但是不清楚为什么这么做
const value = snap.data().original;
console.log('notifying ' + value);
return Promise.all([value])
.then(result => {
const value = result[0].data().value;
...
如果您只想在通知中使用value
的值,则根本不需要使用Promise.all()
,请执行以下操作:
exports.makeUppercase = functions.firestore.document('messages/{messageId}')
.onCreate((snap, context) => {
const value = snap.data().original;
console.log('notifying ' + value);
const payload = {
notification: {
title: "Added",
body: value + "Data Added" //Here we use value
}
};
return admin.messaging().send(payload, false)
.then(result => { //Note that if you don't need the console.log you can get rid of this then()
console.log("Notification sent!");
return null;
});
});