简体   繁体   English

为什么在Mongoose文档上看不到所有JS对象属性?

[英]Why can't I see all JS object properties on a Mongoose document?

I'm writing a route in Express (Node.js) in which i pull some data from mongoose. 我在Express(Node.js)中写一条路由,在其中我从猫鼬中提取了一些数据。 Let's say that at some some point I need to compare if employee._id is in array of bad employees id:: 假设在某些时候,我需要比较employee._id是否包含坏员工ID的数组::

let employees = await EmployeeModel.find().exec();
employees.forEach(function (employee) {
    if (arrayOfBadEmployees.indexOf(employee._id) !== -1) {
        employee.isBad = true;
    }
});
console.log(employees);
console.log(employees[0].isBad);

and here's my output: 这是我的输出:

[ { __v: 0, name: 'Employee X', _id: 1 },
  { __v: 0, name: 'Employee Y', _id: 3 },
  { __v: 0, name: 'Employee Z', _id: 5 } ]
true

So when I can't see 'isBad' property when I console.log the whole array/object, but this property is still there? 因此,当我在console.log整个数组/对象时看不到“ isBad”属性时,此属性仍然存在吗? When i check with propertyIsEnumerable('isBad') it says true. 当我检查propertyIsEnumerable('isBad')时,它说的是true。

Mongoose, by default, returns an instance of MongooseDocument , which doesn't expose your data directly and adds convenience methods like populate or save Mongoose默认情况下会返回MongooseDocument的实例,该实例不会直接公开您的数据,而是会添加便捷的方法,例如populatesave

You can use the lean option to get raw objects instead. 您可以使用lean选项获取原始对象。

MongooseDocument also exposes a toObject function if you need to get editable documents. 如果您需要获取可编辑的文档, MongooseDocument还公开了toObject函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM