【问题标题】:Firebase cloud functions fails on using array's values functionFirebase 云函数无法使用数组的值函数
【发布时间】:2019-06-20 09:45:09
【问题描述】:

我使用 typescript、es2017 目标和 node 8 版本创建了一个 firebase 函数项目。

我正在尝试使用语法:

const items = [1, 2, 3, 4];

const iterator = items.values();

但它失败了:items.values 不是函数

我在 NodeJs 中使用了这种语法,但引发了同样的错误......

有人知道我想让设置解决什么问题吗?

【问题讨论】:

  • 显然你所在的运行时没有实现Array.values()。你确定你set the runtime to be Node 8(afaik 默认仍然是节点 6)?
  • 是的,在管理页面上我可以看到“运行时:Node.js 8 (Beta)”描述。但即使在节点 cli 中也会触发此错误。
  • 我其实不知道Array.values()是在什么运行时引入的,但是本页底部的兼容性表说它是在Node 10.9中添加的:developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
  • 本地,如果我使用node --harmony-array-prototype-values,在 CLI Array.values 上有效(我的节点是 v9.5.0)。但我不知道喜欢在云功能上使用它。
  • 如何在本地运行 Node 8,这是 Cloud Functions 支持的最新版本 (afaik)?

标签: node.js typescript firebase google-cloud-functions


【解决方案1】:

我解决了它,包括 polyfill:

1 - 安装 core-js 包:npm i cores-js --save

2 - 创建 polyfills.ts 文件

import "core-js/es6/symbol";
import "core-js/es6/object";
import "core-js/es6/function";
import "core-js/es6/parse-int";
import "core-js/es6/parse-float";
import "core-js/es6/number";
import "core-js/es6/math";
import "core-js/es6/string";
import "core-js/es6/date";
import "core-js/es6/array";
import "core-js/es6/regexp";
import "core-js/es6/map";
import "core-js/es6/set";
import "core-js/es6/reflect";
import "core-js/es7/reflect";

3 - 在 index.ts 上包含 polyfills.ts 文件导入

导入'./polyfills';

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-01-18
    • 1970-01-01
    • 2019-08-28
    • 2021-01-21
    • 2017-08-14
    • 2019-11-30
    • 2020-07-27
    相关资源
    最近更新 更多