Javascript MongoDB回调未引发引用错误 介绍
所有人都知道,如果调用Javascript MongoDB回调未引发引用错误 介绍,javascript,node.js,mongodb,Javascript,Node.js,Mongodb,所有人都知道,如果调用undefined.test,我们将收到以下错误(NodeJS和Javascript的错误相同): 然后在接下来的几行中,我需要获得shopURL的键,这是一个字符串: var shop = urls.shop || "/"; 我开始添加console.log以查找变量的值: console.log(urls); // undefined var shop = urls.shop || "/"; console.log("Passed");
undefined.test
,我们将收到以下错误(NodeJS和Javascript的错误相同):
然后在接下来的几行中,我需要获得shop
URL的键,这是一个字符串:
var shop = urls.shop || "/";
我开始添加console.log
以查找变量的值:
console.log(urls); // undefined
var shop = urls.shop || "/";
console.log("Passed"); // This isn't run.
我的脚本中的问题是,我重新定义了一个新的URL
变量,该变量使URL
未定义,但问题是:为什么无法读取未定义的的属性“shop”没有出现在这里?因为url
实际上是未定义的
我们知道NodeJS和Javascript中都发生了以下情况:
$ node
> undefined.test
TypeError: Cannot read property 'test' of undefined
at repl:1:11
at REPLServer.self.eval (repl.js:110:21)
at Interface.<anonymous> (repl.js:239:12)
at Interface.EventEmitter.emit (events.js:95:17)
at Interface._onLine (readline.js:202:10)
at Interface._line (readline.js:531:8)
at Interface._ttyWrite (readline.js:760:14)
at ReadStream.onkeypress (readline.js:99:10)
at ReadStream.EventEmitter.emit (events.js:98:17)
at emitKey (readline.js:1095:12)
var a = 10;
foo(function () {
console.log(a); // undefined
var a = 10;
});
function foo(callback) { callback(); }
问题
调试完这个问题后,我发现这个问题来自Mongo:如果我们调用未定义,Mongo内部会回调。有些东西
我们不会得到错误
我创建了一个小脚本来演示这一点:
var mongo = require("mongodb");
// Mongo server
var server = mongo.Server("127.0.0.1", 27017);
var db = new mongo.Db("test", server, { safe: true });
console.log("> START");
// Open database
console.log("Opening database.");
db.open(function(err, db) {
if (err) { return console.log("Cannot open database."); }
// get collection
console.log("No error. Opening collection.");
db.collection("col_one", function(err, collection) {
if(err) { return console.log(err) }
// do something with the collection
console.log("No error. Finding all items from collection.");
collection.find().toArray(function(err, items) {
if(err) { return console.log(err) }
console.log("No error. Items: ", items);
console.log("The following line is: undefined.shop." +
"It will stop the process.");
console.log(undefined.test); // THE ERROR DOES NOT APPEAR!
console.log("> STOP"); // this message doesn't appear.
});
});
});
我的问题是:
为什么错误没有出现?原因是什么?(如果能一起调试MongoDB源代码来找到它,那就太好了。)
调用未定义的时进程为什么停止。某些内容
这怎么解决呢
我已经创建了一个应用程序,您可以在其中下载演示该问题的小应用程序
有趣的是:
如果我们添加一个try{}catch(e){}
语句,我们会发现错误,进程将继续显示STOP
消息
try {
console.log(undefined.test);
} catch (e) {
console.log(e);
}
日志:
查看node mongodb native
驱动程序问题,您会注意到该问题在1.3.18
版本中得到了解决。但是,我对它进行了测试,它没有按预期工作。您没有正确使用process.exit。它接受一个返回码(整数),0
表示成功,非零表示失败。这直接来自C语言和其他与命令行交互的语言。行为描述为@Nirk谢谢!我已经更新了我的问题。>STOP
消息不会出现,因为前一行给出了错误。节点将停止执行并抛出错误。如果您正在捕获错误或收听uncaughtException
,则可能不会显示错误消息。您的代码中有错误处理吗?@user568109您看到错误了吗?它出现了吗?我没看见!这是我的问题。为什么没有出现?升级驱动程序并为db实例上的“error”事件添加事件处理程序,以捕获任何抛出异常。我还建议在将代码投入生产之前,编写单元/集成测试来解决这些问题。你能详细说明你的答案吗?为什么错误没有出现?原因是什么?调用未定义的时进程为什么停止。某些内容
?您能否在一个小节点应用程序中重现该问题?谢谢
try {
console.log(undefined.test);
} catch (e) {
console.log(e);
}
> START
Opening database.
No error. Opening collection.
No error. Finding all items from collection.
No error. Items: []
The following line is: undefined.shop. It will stop the process.
[TypeError: Cannot read property 'test' of undefined]
> STOP