Mongoose find() 返回未定义的属性和奇怪的对象 [英] Mongoose find() returns undefined property and strange object
问题描述
我有一个无法解决的错误,因为这是第一次发生在我身上.
I have a bug that i can't resolve because for the first time it happen to me.
这是我的查询:
Pack.find(
{idclient: clientId }
)
.populate({
path: 'cards',
options: { sort: { 'position': 1 } }
})
. exec(function(err,pack){
if(err){
console.log(err);
}else{
///
// here are my logs
callback(pack);
}
});
当我尝试 console.log(pack) 时,我可以看到 \n 的奇怪返回
When i try console.log(pack), i can see a strange return with \n
{ __v: 1,\n _id: 5596a859240cbd3832123b27,\n grouped: 0,\n idclient: \'4Z8OrisV2AMLZn_lAAAA\',\n matId: 5596a859240cbd3832123b26,\n reversed: 0,\n roomId: 5596a859e37d7e7099cec1e6,\n shuffled: 0,\n type: \'hand\',\n cards: [ 5596a859240cbd3832123b28, 5596a85c240cbd3832123b5d ],\n date: Fri Jul 03 2015 17:20:57 GMT+0200 (CEST),\n iscut: 0 }
通常,我可以看到一个很好的格式化 Json 对象.
usually, i can see a nice formated Json Object.
所以,当我尝试:
console.log(pack.property) => 未定义 ...
console.log(pack.property) => undefined ...
有人遇到过这个问题吗?
anyone has had this problem ?
谢谢
推荐答案
这个分为两部分...
首先,Mongoose find 的回调返回一个数组...... findOne 将返回一个对象.
First, the callback from a Mongoose find returns an array ... findOne will return a single object.
就新行而言,猫鼬文档有一个用于 console.log 的 toString() 助手.它可能会添加换行符以提高可读性.在调用 console.log 之前将输出包装在 JSON.stringify(即 console.log(JSON.stringify(pack)))中,您将看到该文档作为没有换行符的字符串.-http://mongoosejs.com/docs/api.html#document_Document-toString
As far as the new lines go, mongoose documents have a toString() helper for console.log. It is likely adding the newlines for readability. Wrap the output in JSON.stringify (ie. console.log(JSON.stringify(pack))) prior to calling console.log and you will see the document as a string without the newlines. -http://mongoosejs.com/docs/api.html#document_Document-toString
这篇关于Mongoose find() 返回未定义的属性和奇怪的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!