Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/480.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/41.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 节点mongodb错误冒泡&;传播_Javascript_Node.js_Mongodb_Error Handling - Fatal编程技术网

Javascript 节点mongodb错误冒泡&;传播

Javascript 节点mongodb错误冒泡&;传播,javascript,node.js,mongodb,error-handling,Javascript,Node.js,Mongodb,Error Handling,我的Node.js有问题,我的MongoClient.connect()函数需要将抛出的错误传播回main;但是,错误正在丢失,并被解释为uncaughtException var ErrorHandler = require('./bin/lib/error.js').ErrorHandler; //throw new ErrorHandler('Throwing something random.', 'BadRequest', 400); // <-- this works, an

我的
Node.js
有问题,我的
MongoClient.connect()
函数需要将抛出的错误传播回main;但是,错误正在丢失,并被解释为
uncaughtException

var ErrorHandler = require('./bin/lib/error.js').ErrorHandler;

//throw new ErrorHandler('Throwing something random.', 'BadRequest', 400); // <-- this works, and throws error as expected!!

// verify that user doesn't already exist.
MongoClient.connect('mongodb://localhost:27017/testdb', function(err, db){
    if (err) throw new Error( err );                    
    db.collection('users')
      .findOne({'email': useremail}, function(err,doc){
            throw new ErrorHandler('User already exists!', 'BadRequest', 400); // <-- this does not work, and error gets lost as an <anonymous> function error event
       });                              
});
当错误丢失时,它会传播到此处。并在('uncaughtException'),…块中执行,该块按预期记录整个errorstack,但它不会在我希望在服务器对象中捕获的位置被捕获

实际的
console.error(err.stack)
在此实例中打印以下内容:

BadRequest: User already exists!
at new Error (<anonymous>)
at ErrorHandler (/home/cfarmer/dev/norad/bin/lib/error.js:4:18)
at /home/cfarmer/dev/norad/routes.js:118:13
at handleCallback (/home/cfarmer/node_modules/mongodb/lib/utils.js:96:12)
at /home/cfarmer/node_modules/mongodb/lib/collection.js:1353:5
at handleCallback (/home/cfarmer/node_modules/mongodb/lib/utils.js:96:12)
at /home/cfarmer/node_modules/mongodb/lib/cursor.js:670:5
at handleCallback (/home/cfarmer/node_modules/mongodb/node_modules/mongodb-core/lib/cursor.js:154:5)
at nextFunction (/home/cfarmer/node_modules/mongodb/node_modules/mongodb-core/lib/cursor.js:675:5)
at /home/cfarmer/node_modules/mongodb/node_modules/mongodb-core/lib/cursor.js:588:7
BadRequest:用户已存在!
在出现新错误时()
在ErrorHandler(/home/cfarmer/dev/norad/bin/lib/error.js:4:18)
at/home/cfarmer/dev/norad/routes.js:118:13
在handleCallback(/home/cfarmer/node_modules/mongodb/lib/utils.js:96:12)
at/home/cfarmer/node_modules/mongodb/lib/collection.js:1353:5
在handleCallback(/home/cfarmer/node_modules/mongodb/lib/utils.js:96:12)
at/home/cfarmer/node_modules/mongodb/lib/cursor.js:670:5
在handleCallback(/home/cfarmer/node_modules/mongodb/node_modules/mongodb core/lib/cursor.js:154:5)
在下一个函数中(/home/cfarmer/node_modules/mongodb/node_modules/mongodb core/lib/cursor.js:675:5)
at/home/cfarmer/node_modules/mongodb/node_modules/mongodb core/lib/cursor.js:588:7

谢谢

您得到了什么输出?@usandfriends在我的OP中查看我的更新。这可能有助于了解一些情况。我现在的想法是,这与
mongodb
库的异步性质有关。我的服务器响应会在
MongoClient.connect()之前触发并发送回用户
syntax甚至完成了运行;当抛出错误时,它是未捕获的,因为连接已经断开,try/catch块已经退出…你能找到它吗?另外,
ErrorHandler
如何将错误传播回
main
?@usandfriends,不,我还没有找到解决方法ErrorHandler只是通过实现
var err=Error.apply(这是参数)来扩展典型的
Error
我还扩展了
ErrorHandler.prototype
以下内容:
ErrorHandler.prototype=Error.prototype;
然后我只需添加一些我想要捕获的额外条件,比如自定义错误代码和错误名称等。
BadRequest: User already exists!
at new Error (<anonymous>)
at ErrorHandler (/home/cfarmer/dev/norad/bin/lib/error.js:4:18)
at /home/cfarmer/dev/norad/routes.js:118:13
at handleCallback (/home/cfarmer/node_modules/mongodb/lib/utils.js:96:12)
at /home/cfarmer/node_modules/mongodb/lib/collection.js:1353:5
at handleCallback (/home/cfarmer/node_modules/mongodb/lib/utils.js:96:12)
at /home/cfarmer/node_modules/mongodb/lib/cursor.js:670:5
at handleCallback (/home/cfarmer/node_modules/mongodb/node_modules/mongodb-core/lib/cursor.js:154:5)
at nextFunction (/home/cfarmer/node_modules/mongodb/node_modules/mongodb-core/lib/cursor.js:675:5)
at /home/cfarmer/node_modules/mongodb/node_modules/mongodb-core/lib/cursor.js:588:7