Giải pháp
Bạn có thể gọi toObject để truy cập các trường. Ví dụ:
var itemObject = item.toObject();
console.log(itemObject.title); // "foo"
Tại sao
Khi bạn chỉ ra rằng các trường thực được lưu trữ trong _doc trường của tài liệu .
Nhưng tại sao console.log(item) => { title: "foo", content: "bar" } ?
Từ mã nguồn của mongoose (document.js), chúng ta có thể thấy rằng toString phương thức của Document gọi toObject phương pháp. Vì vậy, console.log sẽ hiển thị các trường 'chính xác'. Mã nguồn được hiển thị bên dưới:
var inspect = require('util').inspect;
...
/**
* Helper for console.log
*
* @api public
*/
Document.prototype.inspect = function(options) {
var isPOJO = options &&
utils.getFunctionName(options.constructor) === 'Object';
var opts;
if (isPOJO) {
opts = options;
} else if (this.schema.options.toObject) {
opts = clone(this.schema.options.toObject);
} else {
opts = {};
}
opts.minimize = false;
opts.retainKeyOrder = true;
return this.toObject(opts);
};
/**
* Helper for console.log
*
* @api public
* @method toString
*/
Document.prototype.toString = function() {
return inspect(this.inspect());
};