【问题标题】:ESLint error trying to deploy functions Firebase尝试部署功能 Firebase 时出现 ESLint 错误
【发布时间】:2018-07-12 22:20:26
【问题描述】:

我正在尝试部署 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

我看一下firebase here提供的原始最终结果

我是 Javascript 和这个世界的新手,所以我真的迷失了这种类型的消息。

我该如何解决这个问题?

【问题讨论】:

  • 这里有很多针对不同问题的消息。 “确保您安装了最新版本的 node.js 和 npm。” 看起来很清楚。
  • 完全是我的错。我们可以关闭它吗?谢谢@DenysSéguret
  • 即使 npm 错误得到解决,eslint 警告是合法的,并且会继续阻止代码被部署。绝对需要解决这些问题。
  • 我在 Firebase 上部署时遇到这些错误 57:14 警告 Unexpected function expression prefer-arrow-callback 61:12 error 'ref' is already defined no-redeclare

标签: javascript firebase google-cloud-functions eslint firebase-cli


【解决方案1】:

ESLint 警告是合法的。要获得每一个的更多详细信息,您可以使用它们的唯一 ID 搜索它们。例如:

15:3   error  Expected catch() or return                  promise/catch-or-return

这里警告的 id 是promise/catch-or-return。谷歌搜索“eslint promise/catch-or-return”发现a plugin for ESLint将警告描述为:

对未返回的承诺强制使用 catch()

你得到这个是因为你有一个从then返回的承诺,但你从来没有returncatch因为可能的错误。

第二个警告:

15:69  error  Each then() should return a value or throw  promise/always-return

来自同一个 ESLint 插件,描述如下:

在每个 then() 内部返回以创建可读和可重用的 Promise 链。

这要求您在 then 调用的每个函数中返回一个值,即使您没有任何特殊的东西要发送到下一个承诺链。

您可以像这样解决这两个问题:

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 返回的承诺以捕获错误并向客户端发送错误响应。

【讨论】:

    【解决方案2】:

    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 毫秒后超时。

    这样多次超时后,你会得到错误functions can be executed(如上图所示)。这是因为在 Spark 计划(免费套餐计划)中,CPUMilliSecondsDailyNonbillable 有限制。

    如果你在云功能部分点击Stackdriver's Quota Policy,你可以看到GCD(谷歌云平台)的限制

    【讨论】:

      猜你喜欢
      • 2018-07-14
      • 2021-09-25
      • 2021-06-04
      • 1970-01-01
      • 2019-09-30
      • 1970-01-01
      • 2018-12-15
      • 2017-08-06
      • 2021-05-14
      相关资源
      最近更新 更多