Javascript node.js为什么不';你不明白我的错误吗?

Javascript node.js为什么不';你不明白我的错误吗?,javascript,debugging,exception,node.js,error-handling,Javascript,Debugging,Exception,Node.js,Error Handling,在《代码》里面,我有: 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! } 为什么它从不记录“捕获错误”?相反,它将堆栈跟踪和错

在《代码》里面,我有:

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!
}
为什么它从不记录“捕获错误”?相反,它将堆栈跟踪和错误对象打印到屏幕:

function do_stuff(){
    //If I put the throw here, it will catch it! 
    insert_data('abc',function(){
        throw new Error('haha');
    });
}
{stack:[Getter/Setter],
参数:未定义,
类型:未定义,
留言:“哈哈”}
错误:哈哈
at/home/abc/kj/src/api/friends/helper.js:18:23
at/home/abc/kj/src/api/friends/db.js:44:13
在询问时。(/home/abc/kj/src/node_modules/mysql/lib/client.js:108:11)
在Query.emit(events.js:61:17)
在Query._handlePacket(/home/abc/kj/src/node_modules/mysql/lib/Query.js:51:14)
在客户端。_handlePacket(/home/abc/kj/src/node_modules/mysql/lib/Client.js:312:14)
在解析器处。(本地人)
在Parser.emit(events.js:64:17)
at/home/abc/kj/src/node_modules/mysql/lib/parser.js:71:14
在Parser.write(/home/abc/kj/src/node_modules/mysql/lib/Parser.js:576:7)
注意,如果我把掷骰子放在dou stuff()后面,它就会抓住它


即使我将它嵌套在另一个函数中,如何使它被捕获?

这是使用NodeJS的缺点之一。它基本上有两种处理错误的方法;一个是使用try/catch块,另一个是将每个回调函数的第一个参数作为错误传递

问题在于事件循环异步模型。您可以使用“”事件捕获未捕获的错误,但在Node.JS中,使用回调函数的第一个参数来显示是否存在任何错误已成为一种常见的程序范例,例如:(我以前从未在NodeJS中使用过MySQL,只是举一个一般示例)


insert_data('abc'){throw new Error('haha')}
应该是什么?这不是有效的语法。你的代码到底是什么样子的?@RightSaidFred谢谢,修复了。@TIMEX你不能捕捉异步环境的错误,它不能那样工作。停止使用
try catch
@Raynos如何捕获Node.js中的错误?@TIMEX在callbacks上使用
err
参数这不是使用Node.js的缺点之一。这是一个优点,您可以杀死try catch并使用
(错误,数据)
callbacks@DanielUptontry-catch很难看,速度非常慢,不能异步工作,还会使应用程序崩溃completely@Raynostry catch不会使应用程序崩溃。它的目的不是使应用程序崩溃。“慢得要命”,我对此一无所知;异步模型中的try-catch不是可靠的模式。但是,.catch(函数(err){…});应该作为承诺设计模式工作,但遗憾的是。。。似乎很武断:\这里的每个人都很离谱。阅读一下promises/async/await——这是未来的发展方向。是的,async/await允许try/catch块现在处理异步代码,这非常优雅
{ 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)
function getUser( username, callback ){
    mysql.select("SELECT username from ...", function(err,result){
        if( err != null ){
            callback( err );
            return;
        }

        callback( null, result[0]);
    });
}    

getUser("MyUser", function(err, user){
    if( err != null )
        console.log("Got error! ", err );
    else
        console.log("Got user!");
});