The objective is to check for duplicate records in a MongoDB and return a new file object (filteredObj) that does not contain records already existed in the DB. Each record has a description key.
hasDuplicateDescription method returns a Promise that resolves to a true or false, true if there is no existing record.description in the DB, false otherwise. However, the returned filteredObj has the original fileObj content, without any filtering done.
const filteredObj = fileObj.filter((record) => {
return this.hasDuplicateDescription(record.description).then(
(res) => {
return res;
}
);
});
return filteredObj;
The solution below was the recommended solution for getting async function to work with array.filter in other posts but it does not work in my case as hasDuplicateDescription is an async function that requires each array element passed in by the array.filter method as its argument.
const results = await Promise.all(your_promises)
const filtered_results = results.filter(res => //do your filtering here)
Appreciate any advice on how to approach this problem.
Thank you!