【问题标题】:Node.JS object prototype may only be an Object or null with RedisNode.JS 对象原型在 Redis 中只能是 Object 或 null
【发布时间】:2015-08-06 05:32:30
【问题描述】:

我一直在尝试为我的 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)

这是我的代码:

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 只是一个带有配置对象的 require('config') 文件。

【问题讨论】:

    标签: javascript node.js redis


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

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

      我通过简单地将 Error 重命名为 Errors 来修复它。

      【讨论】:

      • 是的,这是有道理的——这也是为什么应该尽可能避免使用全局变量的部分原因。仅供参考,Errora standard JavaScript thing
      • 是的,我知道,这就是我改变它的原因。
      • 你到底重命名了什么?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-10-30
      • 1970-01-01
      • 2017-11-29
      • 1970-01-01
      • 2020-09-27
      • 2020-01-08
      • 2019-04-06
      相关资源
      最近更新 更多