Mi problema es que quisiera agregar información extra al momento de enviar la respuesta a la función
function totalKey(line) {
return new Promise(resolve => {
Key.countDocuments({ 'line': line }, async (err, count) => {
resolve(count);
})
});
}
Lo que quisiera hacer sería el conteo en Key que contengan la misma clave de linea en cada una
listLineTotalKey(req, res) {
Line.find({}).sort('_id').exec(async (err, line) => {
if (err) return res.status(500).send({ message: 'Internal Server message' });
if (!line) return res.status(404).send({ message: 'Line Not Found' });
return res.status(200).send({
data: line.map(async l => {
return {
_id: l._id,
count: await totalKey(l._id)
}
})
});
});
},
Estaba pensando en hacer un map regresando el objecto en si, pero me regresa
Objectos vacíos :c
Actualización
intenté hacer lo siguiente
async function totalKey(lines) {
return Promise.all(lines).then(line => {
return line.map(l => {
let aux = {};
aux._id = l._id;
aux.count = Key.countDocuments({ 'line': l._id });
return aux;
});
});
}
Pero me envía el error siguiente
(node:8248) UnhandledPromiseRejectionWarning: TypeError: Converting circular structure to JSON
--> starting at object with constructor 'NativeTopology'
| property 's' -> object with constructor 'Object'
| property 'sessionPool' -> object with constructor 'ServerSessionPool'
--- property 'topology' closes the circle
at JSON.stringify (<anonymous>)
at stringify (D:\Node\catalogo\backend\node_modules\express\lib\response.js:1123:12)
at ServerResponse.json (D:\Node\catalogo\backend\node_modules\express\lib\response.js:260:14)
at ServerResponse.send (D:\Node\catalogo\backend\node_modules\express\lib\response.js:158:21)
at D:\Node\catalogo\backend\src\controllers\line.js:55:36
at processTicksAndRejections (internal/process/task_queues.js:97:5
Promise.all([promesas])