错误无法读取属性' 0'未定义的

时间:2014-09-19 12:41:19

标签: node.js mongodb pug

我正在为我的移动应用构建一个nodejs服务器,当我保存数据时我遇到了问题我可以在我的data.jade中显示它

这是我的错误消息:

Cannot read property '0' of undefined
    at jade_debug.unshift.lineno (eval at <anonymous> (/Users/****/Documents/Node-    server/node_modules/jade/lib/jade.js:160:8), <anonymous>:33:21)
at eval (eval at <anonymous> (/Users/****/Documents/Node-server/node_modules/jade/lib/jade.js:160:8), <anonymous>:65:4)
at res (/Users/****/Documents/Node-server/node_modules/jade/lib/jade.js:161:38)
at Object.exports.render (/Users/****/Documents/Node-server/node_modules/jade/lib/jade.js:257:10)
at Object.exports.renderFile (/Users/****/Documents/Node-server/node_modules/jade/lib/jade.js:293:18)
at View.exports.renderFile [as engine] (/Users/Yanis/Documents/Node-server/node_modules/jade/lib/jade.js:278:21)
at View.render (/Users/****/Documents/Node-server/node_modules/express/lib/view.js:76:8)
at Function.app.render (/Users/****/Documents/Node-server/node_modules/express/lib/application.js:505:10)
at ServerResponse.res.render (/Users/****/Documents/Node-server/node_modules/express/lib/response.js:756:7)
at /Users/****/Documents/Node-server/index.js:44:9

index.js:

app.get('/:collection', function(req, res) {
    var params = req.params;
    collectionDriver.findAll(req.params.collection, function (error, objs) {
        if (error) {
            console.log("mongo db error"+error);
            objs = [];
        }
        else {
            if (req.accepts('html')) {
                res.render('data', {objects: objs, collection: req.params.collection});
            }
            else {
                res.set('Content-Type', 'application/json');
                res.send(200, objs);
            }
       }
   });
});

最后是我的data.jade:

#objects
    table(border=1)
            if objects.length > 0
                - each val, key in object[0]
                    th= key
            - each obj in objects
                tr.obj
                - each val, key in obj
                    td.key= val

正如您所看到的,我的对象不是data.jade中的“define”,我不知道为什么。我不知道问题是来自jade文件还是de .js。 你们中的任何人都有解决方案吗?

1 个答案:

答案 0 :(得分:2)

object[0]应该是objects[0]中的data.jade