Node.js与mongo - 渲染“对象”而不是模板内的数据?

时间:2014-06-20 11:21:51

标签: node.js mongodb

检查我的代码......

var swig = require('swig');
var config = require('../config.json');
var mongojs = require('mongojs');
var ObjectId = mongojs.ObjectId;
var db = require('mongojs').connect(config.mongoDB, config.mongoDBcollections);
var roles;


db.lists.find({type:'roles'}).toArray(function(err, item) {
    roles = item[0].data;
    console.log(roles);
});

var sharetpl = swig.compileFile(__dirname +'/../html/register.html');
exports.index = function(req, res){
    var output = sharetpl({
    'title': 'Roles Itself',
    'roles' : [roles]
});
res.send(output);

};

这是console.log输出......

[ { name: 'Web Developer', code: 'wdv' },
  { name: 'Web Designer', code: 'wds' },
  { name: 'System Archtect', code: 'sar' } ]

这一切都没关系,到目前为止......但是HTML ......

服务器端:< | - {{roles}} - >

客户端html: < | - [object Object],[object Object],[object Object] - >

所以,基本上我一直坚持它...它在客户端渲染“[object Object]”而不是数据本身:(非常沮丧。任何线索?ty!

1 个答案:

答案 0 :(得分:0)

roles是一个对象数组,因此您需要引用数组中每个对象的特定字段。

如果您使用的是Mustache,则语法如下:

{{#roles}}
  {{name}}, {{code}}
{{/roles}}
相关问题