var api_friends_helper = require('./helper.js');
try{
api_friends_helper.do_stuff(function(result){
console.log('success');
};
}catch(err){
console.log('caught error'); //this doesn't hit!
}
Và bên do_stuff
, tôi có:Làm thế nào để node.js không gặp lỗi của tôi?
function do_stuff(){
//If I put the throw here, it will catch it!
insert_data('abc',function(){
throw new Error('haha');
});
}
Tại sao nó không bao giờ nhật ký 'bắt lỗi'? Thay vào đó, nó in stack-trace và đối tượng lỗi màn hình:
{ stack: [Getter/Setter],
arguments: undefined,
type: undefined,
message: 'haha' }
Error: haha
at /home/abc/kj/src/api/friends/helper.js:18:23
at /home/abc/kj/src/api/friends/db.js:44:13
at Query.<anonymous> (/home/abc/kj/src/node_modules/mysql/lib/client.js:108:11)
at Query.emit (events.js:61:17)
at Query._handlePacket (/home/abc/kj/src/node_modules/mysql/lib/query.js:51:14)
at Client._handlePacket (/home/abc/kj/src/node_modules/mysql/lib/client.js:312:14)
at Parser.<anonymous> (native)
at Parser.emit (events.js:64:17)
at /home/abc/kj/src/node_modules/mysql/lib/parser.js:71:14
at Parser.write (/home/abc/kj/src/node_modules/mysql/lib/parser.js:576:7)
Chú ý rằng nếu tôi đặt QUYỀN ném SAU do_stuff(), sau đó nó sẽ bắt nó.
Làm thế nào tôi có thể bắt nó, ngay cả khi tôi đặt nó lồng vào bên trong một chức năng khác?
'insert_data ('abc') {throw new Error ('haha')}' nghĩa là gì? Đó không phải là cú pháp hợp lệ. Mã của bạn thực sự trông như thế nào? – RightSaidFred
@RightSaidFred cảm ơn, đã khắc phục. – TIMEX
@TIMEX bạn không thể bắt lỗi của môi trường không đồng bộ, nó không hoạt động theo cách đó. Ngừng sử dụng 'try catch' – Raynos