【问题标题】:Cloud Functions for Firebase Async Await styleFirebase 异步等待风格的云函数
【发布时间】:2017-11-10 16:10:00
【问题描述】:

看起来 Cloud Functions 不支持 Async-Await 表示法。有没有办法在他们使用 Babel 之前使用它们,或者是否建议使用 Promise?

我当前在 Node 上的函数是这样的:

exports.getToken = async (req, res) => {
  //1. Generate token from Braintree
  const result = await gateway.clientToken.generate();

  //2. Return the client token
  res.json(result.clientToken);
};

【问题讨论】:

    标签: firebase google-cloud-functions


    【解决方案1】:

    Cloud Functions 运行的是 LTS 版本的 node.js,根据documentation,此时该版本为 6.14.0。 node 6.x 支持 EcmaScript 6,does not include async/await

    但是,您可以使用 TypeScript 编写代码并将其转换为 ES5/ES6,这将有效地将 async/await 的使用转换为 Promise。网络搜索表明,也许this plugin 可以用来帮助 Babel 进行类似的转译。

    值得注意的是,Firebase CLI 现在允许您使用 native TypeScript support 初始化一个新的 Cloud Functions 项目,这是 Firebase 团队目前向开发人员推荐的。

    如果您不想使用 TypeScript,您现在还可以选择节点 8(目前处于测试阶段,并且支持纯 JavaScript 的 async/await)作为部署目标。您可以follow the documentation to edit your package.json 表示您的功能应该部署到节点8。

    【讨论】:

    • 使用 babel 将 ES8 代码编译为与 Cloud Functions 一起使用的 ES6 代码也很容易,请参见此处的示例:codeburst.io/es6-in-cloud-functions-for-firebase-2-415d15205468
    • 谢谢,解决了我的问题,但是,如果所有子项都是旧值,则整个分支都将被删除>>我可以保持分支为空吗?
    • 加 1 用于设置“engine”:{“node”:8}。这么快的修复!希望我能 +100。
    【解决方案2】:

    在关注very nice post 并查看this repository 之后,我已经转译了我的 javascript,而不是转译 TypeScript

    基本上你可以这样做:

    npm install -g @babel/cli @babel/core @babel/preset-env
    
    • 更新:
      我在使用 babel 的“7.0.0-beta.51”版本时遇到问题。 “7.0.0-beta.44”还可以。
      切换到稳定版本 6

      npm install --save-dev babel-cli babel-preset-env

    在你的项目文件夹中创建文件 .babelrc

    {
      "presets": [
        ["@babel/env", {
          "targets": {
            "node": "6.11.5"
          }
        }]
      ]
    }
    

    将“functions”文件夹移动到“firebaseFunctions”文件夹,然后运行

    babel firebaseFunctions --out-dir functions --copy-files --ignore firebaseFunctions/node_modules
    

    或者为每个你想要转换的文件运行这个命令

    babel originalfile.js --out-file transpiledfile.js
    

    【讨论】:

      【解决方案3】:

      现在您可以通过在functions/package.json 中添加以下内容来使用 Node.js 版本 8

      "engines": {
         "node": "8"
      }
      

      示例:https://github.com/firebase/functions-samples/blob/Node-8/authenticated-json-api/functions/package.json

      【讨论】:

      • 很奇怪。我不必那样做
      • gcloud 部署需要添加--runtime nodejs8 到gcloud,使用firebase 工具部署函数不需要添加
      • 这很好用,现在可能需要被接受为响应,因为函数在运行时也使用节点 8
      • 更新运行节点到8也解决问题。
      • 谢谢,节省了我一些时间。
      【解决方案4】:

      在您的functions/.eslintrc.json 文件中设置为'ecmaVersion': 2017 这将消除 eslint 语法错误

      "parserOptions": {
          "ecmaVersion": 2017
       },
      

      在您的functions/package.json 文件中,通过在下面添加将node 版本设置为8

      "engines": {
           "node": "8"
      },
      

      这会将云节点版本更新为 8 默认节点版本为 6

      【讨论】:

        【解决方案5】:

        上述解决方案不适用于我一个人。我必须更新到最新的 firebase 工具:

        npm update -g firebase-tools
        

        然后更新我的 package.json 添加:

         "engines": {"node": "8"}
        

        使用 async/await 一切正常。

        感谢此博客https://howtofirebase.com/cloud-functions-migrating-to-node-8-9640731a8acc

        【讨论】:

          【解决方案6】:

          正如@adam 所说,解决了我重新安装/升级全局firebase包的问题

          区别在于我的情况是使用 NVM (节点版本管理器)。 不知何故,我的默认节点(v13.x)有firebase-tools,但我没有在项目节点(v10/8)全局安装 所以首先:

          nvm use 10
          

          然后:

          npm i -g firebase-tools
          

          以正确的节点版本重新安装使我的异步功能正常工作。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2021-04-16
            • 2023-03-13
            • 2017-11-08
            • 2019-06-02
            • 2017-04-06
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多