javascript Node.JS 对象原型在 Redis 中只能是一个 Object 或 null

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30426420/
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-10-28 12:09:31  来源:igfitidea点击:

Node.JS object prototype may only be an Object or null with Redis

javascriptnode.jsredis

提问by user295583058

I've been trying to setup redis for my Node.JS API and I've been getting this error:

我一直在尝试为我的 Node.JS API 设置 redis,但我收到了这个错误:

util.js:634
  ctor.prototype = Object.create(superCtor.prototype, {
                          ^
TypeError: Object prototype may only be an Object or null: undefined
    at Function.create (native)
    at Object.exports.inherits (util.js:634:27)
    at Object.<anonymous> (/home/<user>/<project>/node_modules/redis/lib/parser/javascript.js:31:6)
    at Module._compile (module.js:460:26)
    at Object.Module._extensions..js (module.js:478:10)
    at Module.load (module.js:355:32)
    at Function.Module._load (module.js:310:12)
    at Module.require (module.js:365:17)
    at require (module.js:384:17)
    at Object.<anonymous> (/home/<user>/<project>/node_modules/redis/index.js:33:14)

Here's my code:

这是我的代码:

console.log('Redis: ' + config.redis.host);
console.log('Redis: ' + config.redis.port);
console.log('Redis: ' + config.redis.pass);

redis       = require('redis');
redisClient = redis.createClient(
    config.redis.port, 
    config.redis.host, 
    {
        auth_pass: config.redis.pass
    }
);

config is simply a require('config') file with a config object.

config 只是一个带有配置对象的 require('config') 文件。

采纳答案by user295583058

I figured out the issue. I had a global variable called Errorwhich conflicted with redis, and also some other dependencies.

我想通了这个问题。我有一个名为的全局变量Error,它与 redis 以及其他一些依赖项发生冲突。

I fixed it by simply re-naming Errorto Errors.

我通过简单地重新命名ErrorErrors.

回答by T.J. Crowder

The problem is that superCtor.prototypeis undefined. The only valid first argument to Object.createis either nullor a non-nullobject reference; you can't use primitives (including undefined).

问题superCtor.prototypeundefined。唯一有效的第一个参数Object.createnull或非null对象引用;您不能使用原语(包括undefined)。

We can't say whysuperCtor.prototypeis undefined from the code you've quoted, but that's the problem with the Object.createcall.

我们无法从您引用的代码中说明为什么superCtor.prototype未定义,但这就是Object.create调用的问题。