2016-04-23 7 views
0

とJSON文書に追加を見ることができません二つ目の印刷物:は、フィールドがNodejsとマングース

{ address: { loc: [ 37.0814402, 15.287517 ], value: 'viale tica' }, tags: [ 'Primi', 'Secondi' ], __v: 0, cap: 96100, ad_number: 15, business_email: '[email protected]', _id: 571b3b78249a2e160b4eee3f }

この文書と結果の配列内の他の人が、フィールド距離なしているのはなぜ?

答えて

1

result[0]は、mongooseドキュメントオブジェクトです。 console.log(result[0])と呼ぶと結果は文字列に変換されます(マングース文書toString関数付き)。しかし、マングース文書にはdistanceフィールドは含まれていません。

あなたは、各ドキュメントオブジェクトに変換する必要があり、あなたがやりたい:

var objects = results.forEach(results, function(res) { 
    res = res.toObject(); 
    res["distance"] = 5; 
    return res; 
}); 
console.log(objects[0].distance); 
console.log(objects[0]); 

http://mongoosejs.com/docs/api.html#document_Document-toObject

+0

感謝を。私はモンゴースのオブジェクトについて考えなかった。 –