套接字io发出生成'instanceof'的右侧不是对象

时间:2018-06-27 21:26:53

标签: javascript sockets

我遇到以下错误

C:\path\app\node_modules\winston\node_modules\cycle\cycle.js:62
                !(value instanceof String)) {
                        ^

TypeError: Right-hand side of 'instanceof' is not an object

在我的

 return io.to(socket.id).emit('friendsInvited', data);

行。 我在许多地方都使用相同的代码,但是错误仅发生在此代码块中。...参见下文:

       socket.on('inviteFriends', function(body) {
                let inviteesArr = body.split(',');
                let emailObjArray = validateInvitees(body)
                console.log("EmailObj Array: " + JSON.stringify(emailObjArray));

                if (emailObjArray.length === 0) {

                    data = {
                        code: String = '1',
                        msg: String = 'failmessage...'
                    };
   **<<FAILS HERE>>**   return io.to(socket.id).emit('friendsInvited', data);
                } else
                {
                 ...function(a,b,f(c){
                      if (x===0) {} else
                       {
    **<<WORKS HERE>>**   return io.to(socket.id).emit('friendsInvited', data);
                       }
              }}
.......

数据始终是相同的data.code和data.msg。除了此代码块之外,此构造还可在其他许多地方使用。我认为我缺少一些非常基本的东西,但看不到它。我以为我遇到范围问题,但我不这么认为。 编辑:这是更多的堆栈跟踪.....

TypeError: Right-hand side of 'instanceof' is not an object
    at derez (C:\path\app\node_modules\cycle\cycle.js:62:25)
    at Object.decycle (C:\path\app\node_modules\cycle\cycle.js:101:6)
    at Object.exports.clone (C:\path\app\node_modules\winston\lib\winston\common.js:97:22)
    at Object.exports.log (C:\path\app\node_modules\winston\lib\winston\common.js:149:19)
    at exports.Console.Console.log (C:\\path\app\node_modules\winston\lib\winston\transports\console.js:99:19)
    at exports.Console.Transport.logException (C:\path\app\node_modules\winston\lib\winston\transports\transport.js:134:8)
    at logAndWait (C:\path\app\node_modules\winston\lib\winston\logger.js:649:15)
    at C\path\app\\node_modules\winston\node_modules\async\lib\async.js:157:13
    at _each (C:\path\app\node_modules\winston\node_modules\async\lib\async.js:57:9)
    at Object.async.each (C:\path\app\node_modules\winston\node_modules\async\lib\async.js:156:9)
    at exports.Logger.Logger._uncaughtException (C:\path\app\node_modules\winston\lib\winston\logger.js:672:9)
    at emitOne (events.js:116:13)
    at process.emit (events.js:211:7)
    at process._fatalException (bootstrap_node.js:378:26)
npm ERR! code ELIFECYCLE
npm ERR! errno 7
....

1 个答案:

答案 0 :(得分:3)

罪魁祸首在这里:

data = {
  code: String = '1',
  msg: String = 'failmessage...'
};

您基本上将覆盖内置的String构造函数。

示例:

const x = String = 'something';

x等于'something',但这是从String = 'something'赋值返回的结果,实际上这会更改内置的String构造函数。现在,当您尝试使用variableName instanceof String时,String将是一个原始值,但是instanceof只能与构造函数一起用作右侧-因此,由{运行时。