gpt4 book ai didi

javascript - 将函数部署到 Firebase 时出错

转载 作者:行者123 更新时间:2023-12-03 01:30:47 27 4
gpt4 key购买 nike

我正在尝试将功能部署到 Firebase

$ npm install firebase-tools
$ login firebase
$ firebase init functions

然后$firebasedeploy--onlyfunctions$firebasedeploy

package.json:

{
"name": "functions",
"description": "Cloud Functions for Firebase",
"scripts": {
"lint": "eslint .",
"serve": "firebase serve --only functions",
"shell": "firebase functions:shell",
"start": "npm run shell",
"deploy": "firebase deploy --only functions",
"logs": "firebase functions:log"
},
"dependencies": {
"firebase-admin": "~5.12.1",
"firebase-functions": "^1.0.3"
},
"devDependencies": {
"eslint": "^4.12.0",
"eslint-plugin-promise": "^3.6.0"
},
"private": true
}

firebase.json:

{
"functions": {
"predeploy": [
"npm --prefix \"%RESOURCE_DIR%\" run lint"
]
}
}

$RESOURCE_DIR 编辑为 %RESOURCE_DIR% 以避免:

Error: functions predeploy error: Command terminated with non-zero exit code4294963238

我的函数index.js:

const functions = require('firebase-functions');
var admin = require('firebase-admin');

admin.initializeApp(functions.config().firebase);
var wrotedata;
exports.Pushtrigger = functions.database.ref('/messages/{messageId}').onWrite((event) => {
wrotedata = event.data.val();

admin.database().ref('/pushtokens').orderByChild('uid').once('value').then((alltokens) => {
var rawtokens = alltokens.val();
var tokens = [];
this.processtokens(rawtokens).then((processedtokens) => {

for (var token in processedtokens) {
tokens.push(token.devtoken);
}

var payload = {
"notification": {
"title": "Notification title",
"body": "Notification body",
"sound": "default",
"click_action": "FCM_PLUGIN_ACTIVITY",
"icon": "fcm_push_icon"
},
"data": {
"message": "value1",
"sendername": "value2"
}
}
return admin.messaging().sendToDevice(tokens, payload).then((response) => {
console.log('Push notifications');
}).catch((err) => {
console.log(err);
})
})
})
})

function processtokens(rawtokens) {
var promise = new Promise((resolve, reject) => {
var processedtokens = []
for (token in rawtokens) {
processedtokens.push(rawtokens[token]);
}
resolve(processdatatokens);
})
return promise;
}

我已经尝试过:

npm install firebase-admin@latest firebase-functions@latest
npm install -g git://github.com/firebase/firebase-tools#master

但是我得到了:

Error: functions predeploy error: Command terminated with non-zero exit code1

2018-07-12T23_38_09_788Z-调试:

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 '--prefix',
1 verbose cli 'C:\\Users\\User\\Desktop\\pushcloud\\functions',
1 verbose cli 'run',
1 verbose cli 'lint' ]
2 info using npm@5.6.0
3 info using node@v8.9.4
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: C:\Program Files\nodejs\node_modules\npm\node_modules\npm-lifecycle\node-gyp-bin;C:\Users\User\Desktop\pushcloud\functions\node_modules\.bin;C:\ProgramData\Oracle\Java\javapath;C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;C:\Program Files (x86)\Embarcadero\Studio\16.0\bin;C:\Users\Public\Documents\Embarcadero\Studio\16.0\Bpl;C:\Program Files (x86)\Embarcadero\Studio\16.0\bin64;C:\Users\Public\Documents\Embarcadero\Studio\16.0\Bpl\Win64;C:\Inprise\vbroker\bin;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\Python27;C:\Python27\Scripts\;C:\Users\User\AppData\Local\Programs\Python\Python35-32;C:\Users\User\AppData\Local\Programs\Python\Python35-32\Scripts;C:\Program Files\Microsoft SQL Server\130\Tools\Binn\;C:\Emgu\emgucv-windesktop 3.1.0.2282\bin\x86;C:\Program Files (x86)\Skype\Phone\;C:\php;C:\Apache2;C:\Apache2\bin;C:\Program Files\dotnet\;C:\WINDOWS\system32\config\systemprofile\.dnx\bin;C:\Program Files\Microsoft DNX\Dnvm\;C:\Program Files\Java\jdk1.8.0_151\bin;C:\Users\User\AppData\Local\Android\Sdk\platform-tools;C:\Program Files\nodejs\;C:\Program Files\Git\cmd;C:\Program Files\Git\usr\bin;C:\Go\bin;C:\Program Files (x86)\Java\jre1.8.0_101\bin;C:\Users\User\AppData\Local\Programs\Python\Python35-32\Scripts\;C:\Users\User\AppData\Local\Programs\Python\Python35-32\;C:\Users\User\AppData\Local\Microsoft\WindowsApps;C:\Program Files\Microsoft VS Code\bin;C:\Users\User\AppData\Roaming\npm;C:\Users\User\go\bin
9 verbose lifecycle functions@~lint: CWD: C:\Users\User\Desktop\pushcloud\functions
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 Files\nodejs\node_modules\npm\node_modules\npm-lifecycle\index.js:285:16)
13 verbose stack at emitTwo (events.js:126:13)
13 verbose stack at EventEmitter.emit (events.js:214:7)
13 verbose stack at ChildProcess.<anonymous> (C:\Program Files\nodejs\node_modules\npm\node_modules\npm-lifecycle\lib\spawn.js:55:14)
13 verbose stack at emitTwo (events.js:126:13)
13 verbose stack at ChildProcess.emit (events.js:214:7)
13 verbose stack at maybeClose (internal/child_process.js:925:16)
13 verbose stack at Process.ChildProcess._handle.onexit (internal/child_process.js:209:5)
14 verbose pkgid functions@
15 verbose cwd C:\Users\User\Desktop\pushcloud
16 verbose Windows_NT 10.0.16299
17 verbose argv "C:\\Program Files\\nodejs\\node.exe" "C:\\Program Files\\nodejs\\node_modules\\npm\\bin\\npm-cli.js" "--prefix" "C:\\Users\\User\\Desktop\\pushcloud\\functions" "run" "lint"
18 verbose node v8.9.4
19 verbose npm v5.6.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 ]

代码看起来有问题,因为默认 Hello from Firebase! 部署成功

编辑:

如果在 firebase.json 表单中将 \"%RESOURCE_DIR%\" 编辑为 "%RESOURCE_DIR%" ,错误为

Error: There was an error loading firebase.json: Unexpected token '%' "npm --prefix "%RESOURCE_DIR%" run lint"

如果我从 firebase.json 中删除行并在终端中运行 npm --prefix "%RESOURCE_DIR%"run lint:

npm ERR! enoent ENOENT: no such file or directory, open 'C:\Users\User\Desktop\pushcloud\%RESOURCE_DIR%\package.json'
npm ERR! enoent This is related to npm not being able to find a file.
npm ERR! enoent
npm ERR! A complete log of this run can be found in:
npm ERR! C:\Users\User\AppData\Roaming\npm-cache\_logs\2018-07-13T11_33_25_593Z-debug.log

但当 package.json 位于函数目录中时,目录文件夹作为 %RESOURCE_DIR% 与文件夹 etc 一起存在。如果我将其重新定位到 %RESOURCE_DIR%:

npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! functions@ lint: `eslint .`
npm ERR! Exit status 1
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 WARN Local package.json exists, but node_modules missing, did you mean to install?
npm ERR! A complete log of this run can be found in:
npm ERR! C:\Users\User\AppData\Roaming\npm-cache\_logs\2018-07-13T11_50_15_739Z-debug.log

并使用$ firebase deploy --only 函数:

C:\Users\User\Desktop\pushcloud\functions\index.js
9:5 error Expected catch() or return promise/catch-or-return
9:80 error Each then() should return a value or throw promise/always-return
12:9 error Expected catch() or return promise/catch-or-return
12:9 warning Avoid nesting promises promise/no-nesting
33:24 warning Avoid nesting promises promise/no-nesting
33:24 warning Avoid nesting promises promise/no-nesting
33:77 error Each then() should return a value or throw promise/always-return

✖ 7 problems (4 errors, 3 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! C:\Users\User\AppData\Roaming\npm-cache\_logs\2018-07-13T11_51_50_784Z-debug.log
Error: functions predeploy error: Command terminated with non-zero exit code1

最佳答案

您的预部署标志 npm --prefix\"%RESOURCE_DIR%\"run lint 导致了您的问题。

如果您在终端中运行 npm --prefix "%RESOURCE_DIR%"run lint ,它是否正确运行而没有错误?

如果您从 firebase.json 中删除该行,那么它应该可以毫无问题地部署。

EDIT

检查您收到的错误消息,它可以为您提供答案。您的代码 linter 失败,您需要先修复这些错误,然后才能部署。

9:5   error    Expected catch() or return                  promise/catch-or-return
9:80 error Each then() should return a value or throw promise/always-return
12:9 error Expected catch() or return promise/catch-or-return
12:9 warning Avoid nesting promises promise/no-nesting
33:24 warning Avoid nesting promises promise/no-nesting
33:24 warning Avoid nesting promises promise/no-nesting
33:77 error Each then() should return a value or throw promise/always-return

我快速浏览了你的代码,我想我已经清理了它。此外,您不需要将 processtokens 设为 promise ,它是一个同步函数。

const functions = require('firebase-functions');
var admin = require('firebase-admin');

admin.initializeApp(functions.config().firebase);
var wrotedata;
exports.Pushtrigger = functions.database.ref('/messages/{messageId}').onWrite((event) => {
wrotedata = event.data.val();

return admin.database().ref('/pushtokens')
.orderByChild('uid')
.once('value')
.then((alltokens) => {
var rawtokens = alltokens.val();
var tokens = [];

var processedtokens = this.processtokens(rawtokens);

for (var token in processedtokens) {
tokens.push(token.devtoken);
}

var payload = {
"notification": {
"title": "Notification title",
"body": "Notification body",
"sound": "default",
"click_action": "FCM_PLUGIN_ACTIVITY",
"icon": "fcm_push_icon"
},
"data": {
"message": "value1",
"sendername": "value2"
}
};

return admin.messaging().sendToDevice(tokens, payload).then((response) => {
console.log('Push notifications');
}).catch((err) => {
console.log(err);
})
})
})

function processtokens(rawtokens) {
var processedtokens = []
for (token in rawtokens) {
processedtokens.push(rawtokens[token]);
}

return processdatatokens;
}

关于javascript - 将函数部署到 Firebase 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51315948/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com