Im new to async/await. when Im calling the function, the first console.log(newArr) prints the array elements but the second one is empty array and when I return the array that is empty too. Can somebody please help me where Im going wrong.
const prefixPM = 'PM';
const decryptComment = (comment) => {
const data = decrypt(comment).then(function (result) {
const buf2 = Buffer.from(result, 'base64').toString('ascii');
return buf2;
});
return data;
};
const pmData = await queryInterface.sequelize.query(
`select ram.id, ra.name from rater_attributes ra
inner join rater_attribute_mappings ram
on ra.id = ram.raterAttributeId
inner join attributes a
on a.id = ram.by
where a.name='Project Manager'`,
{ raw: true, type: Sequelize.QueryTypes.SELECT },
);
const createPMAttributes = (ratingId, rating) => {
const newArr = [];
pmData.map(async (data) => {
const attribute = `${prefixPM}${data.name}`;
let pmComment = rating.PMComment
? await decryptComment(rating.PMComment)
: null;
pmComment = JSON.parse(pmComment);
newArr.push({
ratingId: ratingId,
raterAttributeMappingId: data.id,
rating: rating[`${attribute}`],
comment:
pmComment && pmComment[`${attribute}Comment`] ? pmComment[`${attribute}Comment`] : null,
createdAt: rating.createdAt,
updatedAt: rating.updatedAt,
});
console.log(newArr) -- this prints the newArr elements
});
console.log(newArr); -- this prints empty arr
return newArr
};