【发布时间】:2017-06-18 22:15:54
【问题描述】:
我正在尝试将员工的 MongoDB 集合显示到 Pug 中。我知道这与第一个“数据”对象有关。我不知道如何在数组中呈现数据。
MongoDB 集合:
{
"data":[
{
"active":true,
"userName":"example",
"employeeDetails":{
"personalDetails":{
"firstName":"Dennis",
"lastName":"Glover",
"emailAddress":"example@example.com"
}
}
},
{
"active": false,
"userName": example2,
"employeeDetails": {
"personalDetails": {
"firstName": "Ben",
"lastName": "Dover",
"emailAddress": "example2@example.com"
}
}
},
]
}
快递:
MongoClient.connect(url, function(err, db) {
if (err) {
console.log('Unable to connect to the Server', err);
} else {
console.log('Connection established to', url);
var employeecollection = db.collection('employees');
// Find all employees
employeecollection.find({}).toArray(function(err, employeeResult) {
if (err) {
res.send(err);
} else if (employeeResult.length) {
res.render('employeelist', {
'employeelist': employeeResult,
});
} else {
res.send('No documents found');
}
db.close();
});
};
});
帕格:
table
each employee in employeelist
tr#employee_list_item
td #{employee.userName}
我已经使用 ng-repeat 将它与 Angular2 一起使用,但是我似乎无法让它在 Pug 中呈现,除非我删除数组中的“数据”对象(它需要存在)。
【问题讨论】:
-
你能告诉我
console.log(employeeResult)的结果吗? -
当然,我得到以下信息:[ { _id: 5892b3ae79fb9d023308dce7, data: [ [Object], [Object], [Object], [Object], [Object], [Object], [Object ], [对象], [对象], [对象], [对象], [对象], [对象], [对象], [对象], [对象], [对象], [对象], [对象], [对象],[对象],[对象],[对象],[对象],[对象]]}]
标签: node.js mongodb express mongoose pug