
[英]Mongoose doesn't execute save and doesn't display error
[英]Error in callback doesn't display error info
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]未定義
不顯示錯誤,僅顯示警告。
這些錯誤發生了很多次。 僅幫助一件事-將所有回調都轉換為異步函數。 然后我可以看到錯誤。
錯誤:
function some_method() {
User.find().limit(1).exec(function(err, users) {
if(err) console.log(err);
console.log(users[1].id)
// i will get the error "WARNING: Something..."
// because i am querying just 1 User, and trying to log second user
});
}
好:
async function some_method() {
var users = await User.find().limit(1);
console.log(users[1].id)
// i will see the exact error , which shows exactly where is the problem
// and say, that i am trying to read property id of undefined.
// You dont even need to console the Errors - it does it automatically!
}
但是,如果您的整個項目都充滿了回調,則至少可以幫助您發現問題何時出現。 由於此消息無助於根本不知道問題出在哪里。 它可以在整個項目中的任何地方。 您可以做的- 臨時處理這些問題,直到從回調到異步替換所有代碼為止。
1)創建測井系統。 記錄每個請求。 我正在使用溫斯頓。 記錄每個請求,以毫秒為單位寫入請求時間。 如果使用pm2,請不要將這些請求日志寫入控制台,因為那樣您就看不到何時發生真正的錯誤。
下一個:
2)覆蓋此警告以寫它出現的時間。 轉到
node_modules/parley/lib/private/Deffered.js
,在2個地方編寫了此警告。 替換為:(new Date()) + '---' + (new Date().getTime()) + '---WARNING: Something seems to be wrong with this function...
因此,現在您至少可以知道哪個請求是錯誤的。 如果項目幾乎被放棄,這種方法會有所幫助。 但是,如果項目處於活動狀態,則應將每個方法重寫為異步等待狀態。 我已經花了6個月的時間-將整個后端重寫為async-await,它確實非常有用-代碼更好,所有錯誤都非常清楚。
如果使用create,update或find,請使用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 () {}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.