回调错误不显示错误信息

时间:2018-10-12 14:28:32

标签: node.js sails.js

Sails.js 1.0.2

嗨。我尝试调试回调中的错误,但唯一的消息是此消息:

    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    WARNING: Something seems to be wrong with this function.
    It is trying to signal that it has finished AGAIN, after
    already resolving/rejecting once.
    (silently ignoring this...)

    To assist you in hunting this down, here is a stack trace:

    ```
    at /node/zg-sails-v1/node_modules/sails-mongo/lib/private/do-with-connection.js:242:28
    at /node/zg-sails-v1/node_modules/sails-mongo/lib/private/do-with-connection.js:123:18
    at Object.success (/node/zg-sails-v1/node_modules/sails-mongo/lib/private/build-std-adapter-method.js:61:47)
    at /node/zg-sails-v1/node_modules/machine/lib/private/help-build-machine.js:1509:30
    at proceedToFinalAfterExecLC (/node/zg-sails-v1/node_modules/parley/lib/private/Deferred.js:1151:14)
    at proceedToInterceptsAndChecks (/node/zg-sails-v1/node_modules/parley/lib/private/Deferred.js:909:12)
    at proceedToAfterExecSpinlocks (/node/zg-sails-v1/node_modules/parley/lib/private/Deferred.js:841:10)
    at /node/zg-sails-v1/node_modules/parley/lib/private/Deferred.js:303:7
    at /node/zg-sails-v1/node_modules/machine/lib/private/help-build-machine.js:964:24
    at Function.handlerCbs.success (/node/zg-sails-v1/node_modules/machine/lib/private/help-build-machine.js:824:26)
    at findCb (/node/zg-sails-v1/node_modules/sails-mongo/lib/private/machines/find-records.js:138:20)
    at handleCallback (/node/zg-sails-v1/node_modules/mongodb/lib/utils.js:120:56)
    at /node/zg-sails-v1/node_modules/mongodb/lib/cursor.js:860:16
    at handleCallback (/node/zg-sails-v1/node_modules/mongodb-core/lib/cursor.js:171:5)
    at setCursorDeadAndNotified (/node/zg-sails-v1/node_modules/mongodb-core/lib/cursor.js:505:3)
    at nextFunction (/node/zg-sails-v1/node_modules/mongodb-core/lib/cursor.js:651:7)
    at Cursor.next [as _next] (/node/zg-sails-v1/node_modules/mongodb-core/lib/cursor.js:692:3)
    at fetchDocs (/node/zg-sails-v1/node_modules/mongodb/lib/cursor.js:856:10)
    at /node/zg-sails-v1/node_modules/mongodb/lib/cursor.js:879:7
    at handleCallback (/node/zg-sails-v1/node_modules/mongodb-core/lib/cursor.js:171:5)
    at nextFunction (/node/zg-sails-v1/node_modules/mongodb-core/lib/cursor.js:682:5)
    at /node/zg-sails-v1/node_modules/mongodb-core/lib/cursor.js:593:7
    ```

我只想显示错误信息。后面有堆栈跟踪,但对我没有帮助。

这是我的代码示例:

User.find({id: id}).exec((err, user) => {
  if (err) {
    ...
  } else {
    ...
    console.log(myUndefinedVar);
  }
});

这是一个具有数百个回调的遗留项目,因此我无法通过等待,尝试/捕获来重构所有代码。

谢谢。

编辑:添加了Stacktrace


编辑:新示例,eslint不挂出电话。

Game.find({id: req.session.gameID}).exec((err, objGame) => {
    if (err) {
        utilService.newLog('error', stackTrace, __line, 'Game.find({id: req.session.gameID: ' + req.session.gameID + ' ...');
        utilService.newLog('error', stackTrace, __line, err);
        return res.redirect('dashboard');
    } else if (typeof objGame === 'undefined') {
        utilService.newLog('error', stackTrace, __line, 'objGame === undefined ...');
        utilService.newLog('error', stackTrace, __line, err);
        return res.redirect('dashboard');
    } else {
        ...
    }
});

Game.find({id: req.session.gameID}).exec((err, objGame) => {

objGame是一个数组

    } else if (typeof objGame === 'undefined') {

    ... 

    jsfiles = sails.config.gameCfg[objGame.type].dev.jsfiles;

必须为

    } else if (typeof objGame[0] === 'undefined') {

因为objGame [0]未定义

没有显示错误,只有警告。

2 个答案:

答案 0 :(得分:0)

如果您使用创建,更新或查找,请使用await

var new game = await Game.find({id: req.session.gameID}).exec((err, objGame) => {
    if (err) {
        utilService.newLog('error', stackTrace, __line, 'Game.find({id: req.session.gameID: ' + req.session.gameID + ' ...');
        utilService.newLog('error', stackTrace, __line, err);
        return res.redirect('dashboard');
    } else if (typeof objGame === 'undefined') {
        utilService.newLog('error', stackTrace, __line, 'objGame === undefined ...');
        utilService.newLog('error', stackTrace, __line, err);
        return res.redirect('dashboard');
    } else {
        ...
    }
});

但是请确保您的函数是异步函数fn: async function () {}

答案 1 :(得分:0)

这些错误发生了很多次。仅帮助一件事-将所有回调都转换为异步函数。然后我可以看到错误了。

错误:

function some_method() {
  User.find().exec(function(error, users) {
    // users_list length  is 1.
    // i am trying to log not users[0], but users[1].
    // i will get the error "WARNING: Something seems to be wrong with this function."
    // and i cant understand where in my whole project the error is..
    console.log(users[1].id)
  });
}

好:

async function some_method() {
  var users = await User.find();
  // users_list length is 1.
  // i am trying to log not users[0], but users[1].
  // i will get the error , which shows exactly where is the problem
  // and say, that i am trying to read property id of undefined.
  console.log(users[1].id)
}