Express.js + mongoose:回退到错误的 ID?

发布于 2024-11-10 07:14:41 字数 1650 浏览 0 评论 0原文

我使用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

泪意 2024-11-17 07:14:41

对于不存在的 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) to if (!err && user_).

心房敞 2024-11-17 07:14:41

尝试切换回调参数>

User.findById(user_id, function(user_, err ) {
    if (!err) {
        res.render('track', { pins: user_.pins, layout: false });
    } else {
        res.send("FAIL! " + err);
    }
});

try switching the callback params >

User.findById(user_id, function(user_, err ) {
    if (!err) {
        res.render('track', { pins: user_.pins, layout: false });
    } else {
        res.send("FAIL! " + err);
    }
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文