Javascript 类型错误:Object.entries 不是函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43858714/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 02:12:41  来源:igfitidea点击:

TypeError: Object.entries is not a function

javascriptnode.jsexpressecmascript-next

提问by

Why do I keep getting this error when trying to run my Node.js/Express server?

为什么我在尝试运行我的 Node.js/Express 服务器时总是收到这个错误?

Is this a part of the newer ES7? What do I need to be able to run an app using these new features?

这是新 ES7 的一部分吗?我需要什么才能使用这些新功能运行应用程序?

回答by derp

According to http://kangax.github.io/compat-table/es2016plus/under the Object static methods, it seems you need to enable the harmony flag

根据http://kangax.github.io/compat-table/es2016plus/下的Object静态方法,看来你需要启用和谐标志

So run node like this

所以像这样运行节点

node --harmony script.js

回答by peja

On mdn docs, there is a clear tutorial on Object.entries, and it is described what to be done if Object.entries is not supported on part PolyFillin the same page.

在 mdn 文档上,有一个关于Object.entries的清晰教程,并且描述了如果同一页面中的部分PolyFill不支持 Object.entries 时该怎么做。

To add compatible Object.entries support in older environments that do not natively support it, you can find a demonstrational implementation of Object.entries in the tc39/proposal-object-values-entries (if you don't need any support for IE), a polyfill in the es-shims/Object.entries repositories, or you can use the simple, ready to deploy polyfill listed below.

if (!Object.entries)
   Object.entries = function( obj ){
      var ownProps = Object.keys( obj ),
         i = ownProps.length,
         resArray = new Array(i); // preallocate the Array

      while (i--)
         resArray[i] = [ownProps[i], obj[ownProps[i]]];
      return resArray;
   };

要在本机不支持的旧环境中添加兼容的 Object.entries 支持,您可以在 tc39/proposal-object-values-entries 中找到 Object.entries 的演示实现(如果您不需要对 IE 的任何支持) , es-shims/Object.entries 存储库中的 polyfill,或者您可以使用下面列出的简单的、准备部署的 polyfill。

if (!Object.entries)
   Object.entries = function( obj ){
      var ownProps = Object.keys( obj ),
         i = ownProps.length,
         resArray = new Array(i); // preallocate the Array

      while (i--)
         resArray[i] = [ownProps[i], obj[ownProps[i]]];
      return resArray;
   };

回答by deepak prakash

you could use babel-polyfillfor quick solution

您可以babel-polyfill用于快速解决方案

npm install babel-polyfill

import 'babel-polyfill';

回答by Snekse

In case this helps someone else...

万一这对其他人有帮助...

Update your version of Node. I was running node 6.x and this issue resolved itself after I updated to node 8.x+

更新您的 Node.js 版本。我正在运行节点 6.x,更新到节点 8.x+ 后,此问题自行解决

回答by lwz7512

First install react-app-polyfill:

首先安装 react-app-polyfill:

npm install react-app-polyfill

Then import to the top of your index.jsx before import React:

然后在导入 React 之前导入到 index.jsx 的顶部:

import 'react-app-polyfill/stable';