当我部署函数时,出现错误,我尝试了所有方法,但无法解决问题我正在写推送通知的功能我认为我写的代码是正确的,但我不确定它是否正确
这是我在CMD 上收到的错误
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! C:Users 00AppDataRoamingnpm-cache_logs2018-07-
20T15_42_18_516Z-debug.log
Error: functions predeploy error: Command terminated with non-zero exit
code1
这是日志文件
0 info it worked if it ends with ok
1 verbose cli [ 'C:\Program Files\nodejs\node.exe',
1 verbose cli 'C:\Program Files\nodejs\node_modules\npm\bin\npm-
cli.js',
1 verbose cli '%RESOURCE_DIR%',
1 verbose cli 'run',
1 verbose cli 'lint' ]
2 info using npm@6.1.0
3 info using node@v10.4.1
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
9 verbose lifecycle functions@~lint: CWD: C:Users 00fb-
functions%RESOURCE_DIR%
10 silly lifecycle functions@~lint: Args: [ '/d /s /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> (C:Program
Filesnodejsnode_modulesnpmnode_modulesnpm-lifecycleindex.js:304:16)
13 verbose stack at EventEmitter.emit (events.js:182:13)
13 verbose stack at ChildProcess.<anonymous> (C:Program
Filesnodejsnode_modulesnpmnode_modulesnpm-
lifecyclelibspawn.js:55:14)
13 verbose stack at ChildProcess.emit (events.js:182:13)
13 verbose stack at maybeClose (internal/child_process.js:961:16)
13 verbose stack at Process.ChildProcess._handle.onexit
(internal/child_process.js:248:5)
14 verbose pkgid functions@
15 verbose cwd C:Users 00fb-functions
16 verbose Windows_NT 10.0.10586
17 verbose argv "C:\Program Files\nodejs\node.exe" "C:\Program
Files\nodejs\node_modules\npm\bin\npm-cli.js" "--prefix"
"%RESOURCE_DIR%" "run" "lint"
18 verbose node v10.4.1
19 verbose npm v6.1.0
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 中看到的代码
'use strict'
const functions = require('firebase-functions');
const admin = require ('firebase-admin');
admin.initializApp(functions.config().firebase);
exports.sendNotification=
functions.database.ref('/Notifications/${receiver_id}/{notification_id}')
.onWrite(event =>{
const receiver_id = event.params.receiver_id;
const notification_id = event.params.notification_id;
console.log('We have a Notifications to send to :',
receiver_id);
if (!event.data.val())
{
return console.log('A Notifications has been deleted from
the database', notification_id);
}
const deviceToken =
admin.database().ref(`/Users/${receiver_id}/device_token`)
.once('value');
return deviceToken.then(response =>
{
const token_id = result.val();
const payload =
{
notifications;
title: "Friend Request",
body: "You have a New Friend",
icon: "default"
return admin.messaging().sendToDevice(token_id, payload)
.then(response =>{
console.log('This was the notification feature.');
});
};
});
});
您使用的是什么版本的云函数?如果您使用的是1.0或更高版本,那么代码中有几件事需要解决。首先,指南指出实时数据库.onWrite
触发器传递两个参数:更改和上下文。此外,路径中不应该有"$"。此外,parameters
对象似乎包含了所有代码的后半部分。在有效载荷中,它写着"notifications;",但我不确定应该是什么。看起来它应该与指南中显示的有效载荷相匹配。可能还有其他错误我没有发现。如果是我,我可能会尝试获得一个更简单的函数来成功部署,然后一块一块地添加到它中。
exports.sendNotification = functions.database.ref('/Notifications/{receiver_id}/{notification_id}')
.onWrite((change, context) => {
const receiver_id = context.params.receiver_id;
const notification_id = context.params.notification_id;
console.log('We have a Notifications to send to :', receiver_id);
if (!change.after.val()) {
return console.log('A Notifications has been deleted from the database', notification_id);
}
const deviceToken = admin.database().ref(`/Users/${receiver_id}/device_token`)
.once('value');
return deviceToken.then(response => {
const token_id = result.val();
const payload = {
notification: {
title: 'Friend Request',
body: 'You have a New Friend',
icon: 'default'
}
};
return admin.messaging().sendToDevice(token_id, payload)
.then(response => {
console.log('This was the notification feature.');
});
});
});