[英]Mongoose & mongoDB with nested queries
我將在mongoDB中執行的查詢傳遞給mongoose,這給了我錯誤。
mongo DB中的查詢為:
db.facturas.find({
_id: {
$in: db.detalles.distinct("id_factura", {
category: ObjectId("5889eae21ffcc6da2c6b4ae4")
})
}
})
在APP中,我建立查詢如下:
Factura.find({'_id': { "$in" : Detalle.distinct("id_factura",{qrycat}) } })
.populate('pto_venta')
.populate('forma_pago')
.exec(function(err, result) {
if (err) res.send(err);
res.json(result);
});
我在NODEJS中收到以下錯誤:
/var/nodejs/aadides-sgi/node_modules/express/lib/response.js:242 var body = JSON.stringify(val,replacer,space); ^
TypeError:在ServerResponse.send(/ var /)的ServerResponse.json(/var/nodejs/aadides-sgi/node_modules/express/lib/response.js:242:19)的Object.stringify(native)處將循環結構轉換為JSON在/var/nodejs/aadides-sgi/app/handlers/facturasHandler.js:494:22處的/var/nodejs/aadides-sgi/nodejs/aadides-sgi/node_modules/express/lib/response.js:151:21) /node_modules/mongoose/lib/query.js:2176:21(位於/var/nodejs/aadides-sgi/node_modules/kareem/index.js:160:11(位於Query._find(/ var / nodejs / aadides-sgi / node_modules) /mongoose/lib/query.js:1019:5)位於/var/nodejs/aadides-sgi/node_modules/kareem/index.js:156:8位於/ var / nodejs / aadides-sgi / node_modules / kareem / index。在process._tickCallback(internal / process / next_tick.js:104:9)的_combinedTickCallback(internal / process / next_tick.js:73:7)的js:18:7 events.js:160 throw er; //未處理的“錯誤”事件^
錯誤:在export._errnoException(util.js:1018:11)處在Pipe.onread(net.js:568:26)處讀取ECONNRESET
仔細檢查您的第二個參數是否有distinct(),因為{qrycat}
看起來不是有效的對象。
更重要的是, distinct()
是異步的(就像大多數貓鼬操作一樣),因此在調用下一個操作之前,您需要回調以獲取ID。
Detalle.distinct("id_factura", {qrycat}, function (err, id_facturas) {
if (err)
return res.send(err);
// id_facturas should be an array
Factura.find({'_id': { $in: id_facturas }})
.populate('pto_venta forma_pago') // Mongoose >= 3.6
.exec(function(err, result) {
if (err)
return res.send(err); // don't forget the return
res.json(result);
});
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.