Express.js + mongoose:回退到错误的 ID?
我使用express.js 和mongoose 来访问mongoDB。如果有人输入不存在的网址(基于 user_id),我尝试实现后备。这是代码:
app.get('/track/:id', function(req, res) {
var user_id = req.params.id;
User.findById(user_id, function(err, user_) {
if (!err) {
res.render('track', { pins: user_.pins, layout: false });
} else {
res.send("FAIL! " + err);
}
});
});
当访问 http://foo.bar/track/1234 且 1234 不存在时,它应该打印出 FAIL!,如果 id 存在,它应该从数据库获取它的内容(这实际上是有效的)。这是我现在输入错误的 ID 时得到的结果(应用程序崩溃):
/home/node/node-service/releases/20110530081158/server.js:112
res.render('track', { pins: user_.pins, layout: false });
^
TypeError: Cannot read property 'pins' of null
at /home/node/node-service/releases/20110530081158/server.js:112:46
at /home/node/.node_libraries/.npm/mongoose/1.3.6/package/lib/mongoose/query.js:778:22
at [object Object].<anonymous> (/home/node/.node_libraries/.npm/mongoose/1.3.6/package/support/node-mongodb-native/lib/mongodb/collection.js:462:35)
at [object Object].emit (events.js:67:17)
at [object Object].<anonymous> (/home/node/.node_libraries/.npm/mongoose/1.3.6/package/support/node-mongodb-native/lib/mongodb/db.js:94:12)
at [object Object].emit (events.js:64:17)
at Socket.<anonymous> (/home/node/.node_libraries/.npm/mongoose/1.3.6/package/support/node-mongodb-native/lib/mongodb/connection.js:86:16)
at Socket.emit (events.js:64:17)
at Socket._onReadable (net.js:671:31)
at IOWatcher.onReadable [as callback] (net.js:177:10)
i use express.js with mongoose for mongoDB access. i try to implement a fallback, if someone enters an url which does not exist (based on the user_id). here's the code:
app.get('/track/:id', function(req, res) {
var user_id = req.params.id;
User.findById(user_id, function(err, user_) {
if (!err) {
res.render('track', { pins: user_.pins, layout: false });
} else {
res.send("FAIL! " + err);
}
});
});
when accessing http://foo.bar/track/1234 and 1234 doesn't exist, it should print out FAIL!, if the id exist it should get it's content from the db (this actually works). this is what i get now when entering an incorrect id (the app crashes):
/home/node/node-service/releases/20110530081158/server.js:112
res.render('track', { pins: user_.pins, layout: false });
^
TypeError: Cannot read property 'pins' of null
at /home/node/node-service/releases/20110530081158/server.js:112:46
at /home/node/.node_libraries/.npm/mongoose/1.3.6/package/lib/mongoose/query.js:778:22
at [object Object].<anonymous> (/home/node/.node_libraries/.npm/mongoose/1.3.6/package/support/node-mongodb-native/lib/mongodb/collection.js:462:35)
at [object Object].emit (events.js:67:17)
at [object Object].<anonymous> (/home/node/.node_libraries/.npm/mongoose/1.3.6/package/support/node-mongodb-native/lib/mongodb/db.js:94:12)
at [object Object].emit (events.js:64:17)
at Socket.<anonymous> (/home/node/.node_libraries/.npm/mongoose/1.3.6/package/support/node-mongodb-native/lib/mongodb/connection.js:86:16)
at Socket.emit (events.js:64:17)
at Socket._onReadable (net.js:671:31)
at IOWatcher.onReadable [as callback] (net.js:177:10)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
对于不存在的 ID 不返回任何数据是正常操作,而不是错误,因此不会设置 err。
尝试将
if (!err)
更改为if (!err && user_)
。Returning no data for an ID that doesn't exist is normal operation, not an error, and so err is not being set.
Try changing
if (!err)
toif (!err && user_)
.尝试切换回调参数>
try switching the callback params >