【问题标题】:Firebase Cloud Function Send Notification ErrorFirebase 云函数发送通知错误
【发布时间】:2019-09-25 22:59:23
【问题描述】:

每当数据添加到 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。在这里的任何帮助将不胜感激。

【问题讨论】:

  • 你为什么要Promise.all([value])value 中有什么内容?
  • @RenaudTarnec 这是我想在通知中显示的值。每当在集合中添加新文档时,我都会从 Firestore 获取它。值是该文档中某个字段的值。
  • 但是你没有在通知负载中使用它。
  • 无论如何,目前还不清楚你为什么要这样做Promise.all([value])。如果我对您的理解正确,您将获得您想要使用snap.data().original 发送的值,因此您无需再使用value 执行任何操作。另外,我不认为 [value] 是一个 Promise 数组(这是您需要传递给 Promise.all() 的内容)。
  • @RenaudTarnec 您在下面的回答为我修复了它。为此非常感谢。我非常感谢您的帮助。

标签: node.js firebase promise google-cloud-firestore google-cloud-functions


【解决方案1】:

Promise.all()admin.messaging().send() 都返回一个 Promise,因此您需要链接这些 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;
    });
});

【讨论】:

    猜你喜欢
    • 2018-06-09
    • 2019-11-29
    • 2018-09-29
    • 2019-02-23
    • 2019-01-30
    • 2021-10-07
    • 2020-09-09
    • 2021-04-04
    • 1970-01-01
    相关资源
    最近更新 更多