i am trying to store data in MongoDB.Here I am checking whether data is present or not. if data already exists then throwing an error or else storing data. please check the below code in the below data having duplicate data(name and ICC). after saving the first record it should throw an error but here it's not throwing any error.
let Records = [{
name: 'test',
icc: 'testId'
},
{
name: 'test',
icc: 'testId'
}]
await Promise.all(Records.map(async (item) => {
try {
item['id'] = id
let ItemObj = table_name.build();
item.name = item.name;
item.icc = item.icc;
let found = await table_name.findOne({ where: { name: item.name, id: id } })
console.log("11111111111111111");
if (found) {
console.log("22222222222222222222");
throw new Error(item.name + " is already exist!");
} else {
console.log("333333333333333333333333333333");
Object.assign(ItemObj.dataValues, item);
console.log("4444444444444444444")
await ItemObj.save()
console.log("5555555555555555555")
}
} catch (err) {
throw new Error(err);
}
})
).then((data) => {
console.log("final");
}).catch((err) => {
console.log("error logged");
})
here flow goes like on console
11111111111111111
33333333333333333
44444444444444444
11111111111111111
33333333333333333
44444444444444444
55555555555555555
55555555555555555