无法读取通过node-mongodb-native检索的对象的属性

时间:2012-02-04 16:00:36

标签: node.js mongodb

编辑:我已经想到这一点,错误来自浏览器请求'favicon.ico',只需检查文件是否已退回

if (doc) res.send(doc.title);

我正在连接MongoHQ数据库并从我的收藏集“CMS”中检索单个文档。

mongodb.connect(mongourl, function(err, db){
    db.collection('CMS', function(err, collection) {
        app.get('/:slug', function(req, res){
            collection.findOne({slug:req.params.slug}, function(err, doc){
                res.send(doc.title);
            })
        });
    });
});

上面的代码在浏览器中显示标题,但随后崩溃了节点,出现此错误:

node.js:201
        throw e; // process.nextTick error, or 'error' event on first tick
              ^
TypeError: Cannot read property 'title' of null
    at /Users/tom/git/cms/app.js:24:17
    at /Users/tom/git/cms/node_modules/mongodb/lib/mongodb/collection.js:962:5
    at /Users/tom/git/cms/node_modules/mongodb/lib/mongodb/cursor.js:131:9
    at /Users/tom/git/cms/node_modules/mongodb/lib/mongodb/cursor.js:173:11
    at /Users/tom/git/cms/node_modules/mongodb/lib/mongodb/cursor.js:454:35
    at Cursor.close (/Users/tom/git/cms/node_modules/mongodb/lib/mongodb/cursor.js:662:5)
    at Cursor.nextObject (/Users/tom/git/cms/node_modules/mongodb/lib/mongodb/cursor.js:454:17)
    at [object Object].<anonymous> (/Users/tom/git/cms/node_modules/mongodb/lib/mongodb/cursor.js:442:12)
    at [object Object].g (events.js:156:14)
    at [object Object].emit (events.js:88:20)

我不清楚为什么它会显示标题,但随后出现错误,说标题属性不存在?有什么建议吗?

0 个答案:

没有答案