I'am new in JS and had some problems with async function. I have a function, that return data from MongoDB. I create a promise, and get collection with empty object.
async function getRating(item, applicant) {
let arr = await Applicant.find({ proff: item }).sort({ ball: "desc" });
let rating = arr.findIndex(item => item._id.equals(applicant._id));
let spec = await Spec.findById(item);
const el = { spec, rating };
return el;}
router.get("/test", auth, async (req, res) => {
let applicant = await Applicant.findOne({ user: req.user.id });
let ratings = [];
applicant.proff.forEach(item => {
const rating = getRating(item, applicant)
.then(rating => ratings.push(rating))
.catch(err => console.log(err));
ratings.push(rating);
});
await res.json(ratings);
});
When I check in Postman, this return me array: [{},{},{}]
Please help.