【问题标题】:Iterate MongoDB collection to pug将 MongoDB 集合迭代到 pug
【发布时间】: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


【解决方案1】:

据我所知,employeelist[0].data 是您要迭代的数组。

将employeelist 更改为employeelist[0].data

table 
each employee in employeelist[0].data
    tr#employee_list_item
        td #{employee.userName}

更新。替代方法:

正如 Mohit 建议的那样,如果您从路线本身发送,那么您的原始代码将起作用。

    // Find all employees
    employeecollection.find({}).toArray(function(err, employeeResult) {
        if (err) {
            res.send(err);
        } else if (employeeResult.length) {
            res.render('employeelist', {
                'employeelist': employeeResult[0].data,
            });
        } else {
            res.send('No documents found');
        }
        db.close();
    });

那么,在您看来: 哈巴狗

table
each employee in employeelist
    tr#employee_list_item    
      td #{employee.userName}

希望对你有帮助!

【讨论】:

  • 谢谢! - 但是我从 Mongoose 在我的浏览器中显示以下错误:无法读取未定义的属性“长度”
  • 应该是employeelist[0].data吧?这可以在路由文件本身中修改,以所需格式发送数据。
  • 是的,应该是employeelist[0].data。我会更新我的答案。而且,是的,如果数据以所需的格式从路由本身发送,那么 OP 的原始代码将起作用。
  • 非常感谢,这已解决!
  • 头发我可以帮你。如果它对你有用,请支持我的答案。
猜你喜欢
  • 1970-01-01
  • 2012-09-25
  • 1970-01-01
  • 2021-12-14
  • 2012-04-02
  • 1970-01-01
  • 2019-06-13
  • 1970-01-01
  • 2017-02-11
相关资源
最近更新 更多