[英]ESLint error trying to deploy functions Firebase
我尝试部署fireabase示例,但是当我尝试部署它时,CLI会启动一个错误:
[码]
const functions = require('firebase-functions'); //to activate firebase functions
const admin = require('firebase-admin'); //to active firebase database permissions
admin.initializeApp(functions.config().firebase);
exports.addMessage = functions.https.onRequest((req, res) => {
// [END addMessageTrigger]
// Grab the text parameter.
const original = req.query.text;
// [START adminSdkPush]
// Push the new message into the Realtime Database using the Firebase Admin SDK.
admin.database().ref('/messages').push({original: original}).then(snapshot => {
// Redirect with 303 SEE OTHER to the URL of the pushed object in the Firebase console.
res.redirect(303, snapshot.ref);
});
// [END adminSdkPush]
});
[错误]
15:3 error Expected catch() or return promise/catch-or-return
15:69 error Each then() should return a value or throw promise/always-return
✖ 2 problems (2 errors, 0 warnings)
npm ERR! Darwin 17.4.0
npm ERR! argv "/usr/local/bin/node" "/usr/local/bin/npm" "--prefix" "/Users/user/test/functions" "run" "lint"
npm ERR! node v6.10.2
npm ERR! npm v3.10.10
npm ERR! code ELIFECYCLE
npm ERR! functions@ lint: `eslint .`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the functions@ lint script 'eslint .'.
npm ERR! Make sure you have the latest version of node.js and npm installed.
npm ERR! If you do, this is most likely a problem with the functions package,
npm ERR! not with npm itself.
npm ERR! Tell the author that this fails on your system:
npm ERR! eslint .
npm ERR! You can get information on how to open an issue for this project with:
npm ERR! npm bugs functions
npm ERR! Or if that isn't available, you can get their info via:
npm ERR! npm owner ls functions
npm ERR! There is likely additional logging output above.
npm ERR! Please include the following file with any support request:
npm ERR! /Users/eliassebastian/Developer/npm-debug.log
Error: functions predeploy error: Command terminated with non-zero exit code1
我看看原始最终由火力提供这里
我是Javascript和全世界的新手,所以我真的迷失了这种类型的消息。
我怎么解决这个问题?
ESLint警告是合法的。 要获得每个详细信息,您可以使用其唯一ID进行搜索。 例如:
15:3 error Expected catch() or return promise/catch-or-return
这里警告的id是promise/catch-or-return
。 谷歌搜索“eslint promise / catch-or-return”找到一个ESLint插件,它将警告描述为:
强制在未返回的promise上使用catch()
你得到这个,因为你有一个承诺then
,但你永远不会return
或catch
可能的错误。
第二个警告:
15:69 error Each then() should return a value or throw promise/always-return
来自相同的ESLint插件,并且描述如下:
返回每个then()以创建可读和可重用的Promise链。
它要求你在then
调用的每个函数中返回一个值,即使你没有任何特殊的东西发送到下一个promise链。
您可以像这样解决这两个问题:
admin.database().ref('/messages').push({original: original}).then(snapshot => {
res.redirect(303, snapshot.ref);
return null;
}).catch(error => {
console.error(error);
res.error(500);
});
请注意, then
块现在返回null,并且catch
用于then
返回的promise以捕获错误并向客户端发送错误响应。
Doug Stevenson的答案部分正确,因为它有一个缺陷。
它应该是 :
admin.database().ref('/messages').push({original: original}).then(snapshot => {
res.redirect(303, snapshot.ref);
return 1; // IT SHOULD RETURN NON-NULL VALUE
}).catch(error => {
console.error(error);
res.error(500);
});
当云函数返回null时,它会进入无限循环 。 结果,我们可以看到在上图中60,000 ms后功能超时。
在这样的多个超时功能之后,您将获得可以执行的错误功能 (如上图所示)。 这是因为在Spark Plan(免费套餐计划)中, CPUMilliSecondsDailyNonbillable有限制。
如果您在云功能部分单击Stackdriver的配额策略 ,您可以在GCD(Google云端平台)中查看限制
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.