【问题标题】:Node.js TypeError: Object function Object() { [native code] } has no method 'assign'Node.js TypeError: Object function Object() { [native code] } has no method 'assign'
【发布时间】:2017-10-09 14:29:58
【问题描述】:

每当我执行我的程序时,我都会收到以下TypeError:

/home/Node-Project/node_modules/sentiment/lib/index.js:31
        afinn = Object.assign(afinn, inject);
                       ^
TypeError: Object function Object() { [native code] } has no method 'assign'
    at module.exports (/home/Node-Project/node_modules/sentiment/lib/index.js:31:24)
    at EventEmitter.<anonymous> (/home/Node-Project/twit4.js:17:9)
    at EventEmitter.emit (events.js:95:17)
    at EventEmitter.processTweet (/home/Node-Project/node_modules/ntwitter/lib/twitter.js:242:14)
    at EventEmitter.emit (events.js:95:17)
    at EventEmitter.receive (/home/Node-Project/node_modules/ntwitter/lib/parser.js:44:12)
    at IncomingMessage.<anonymous> (/home/Node-Project/node_modules/ntwitter/lib/twitter.js:258:16)
    at IncomingMessage.emit (events.js:95:17)
    at IncomingMessage.<anonymous> (_stream_readable.js:765:14)
    at IncomingMessage.emit (events.js:92:17)

我搜索过,但我无法理解这个错误。

我必须安装一个特殊的模块吗?

我必须更新 Node 或 npm 吗?

我的程序有错误吗?

这是我的程序:

var twitter = require('ntwitter');
var credentials = require('./credentials3.js');
var sentiment = require ('sentiment');

var twit = new twitter({
    consumer_key: credentials.consumer_key,
    consumer_secret: credentials.consumer_secret,
    access_token_key: credentials.access_token_key,
    access_token_secret: credentials.access_token_secret
});

twit.stream('statuses/filter',{ 'locations':'loc'}, 
function(stream) {
        stream.on('data', function(tweet) {
        var twitterSentiment, geoColor;
        sentiment(tweet.text, function (err, result) {
            twitterSentiment = result;
            if (result == 0) {
                geoColor = '#B5B5B5';
            } else if (result < 0) {
                geoColor = '#FC0828';
            } else {
                geoColor = '#00DE1E';
            } console.log(result);              
            });

        });
    });

有人可以解释一下吗?

【问题讨论】:

  • “我是否必须更新节点” 很可能。您使用哪个版本?看看node.green,看看你的版本是否支持Object.assign。

标签: javascript node.js


【解决方案1】:

只有 Node.js v4 及更高版本内置了Object.assign。

如果您使用的是旧版本的 Node,您可以使用像 object.assign 这样的 polyfill。

【讨论】:

  • 最好只更新到更现代的 node.js 版本。
【解决方案2】:

在 Cordova 项目中遇到这种错误,添加此代码已解决我的问题;

if (typeof Object.assign != 'function') {
    Object.assign = function (target, varArgs) {
        'use strict';
        if (target == null) {
            throw new TypeError('Cannot convert undefined or null to object');
        }
        var to = Object(target);
        for (var index = 1; index < arguments.length; index++) {
            var nextSource = arguments[index];

            if (nextSource != null) {
                for (var nextKey in nextSource) {
                    if (Object.prototype.hasOwnProperty.call(nextSource, nextKey)) {
                        to[nextKey] = nextSource[nextKey];
                    }
                }
            }
         }
         return to;
    };
}

来源:https://github.com/auth0/auth0-cordova/issues/46#issuecomment-311923820

【讨论】:

    【解决方案3】:

    也许我来得太晚了。我遇到了同样的问题,并像下面这样整理出来。从 nodejs.org 网站下载并安装最新版本的节点。参考视频https://docs.npmjs.com/getting-started/installing-node 并检查节点版本(node -v),希望它应该是 v6+。希望它能解决你的问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-25
      • 1970-01-01
      • 1970-01-01
      • 2013-12-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多