I have a problem when i want an array of collections returned back to me using Mongoose. The problem is that the .map method in the code returns an array of empty objects, but if I log the objects individually in the .map everything is fine. Why is this happening?
const patients = doctor.patients.map(async patient => {
try {
const patientObj = await Patient.findOne({ username: patient });
patient = patientObj;
patient.jwt = undefined;
patient.__v = undefined;
console.log(patient); // This works just fine, logs the object the right way
return patient;
} catch (err) {
console.log(err);
}
});
console.log(patients); // This logs [{}, {}, {}]
returnin your mapping function. 2. You pass an async callback so you'd get an array of promises back regardless of what code you have.