I'm using Hapi Js and Sequelize to do something about my API and in this case I need to check everything first before go the next step.
This is my code
return promise.map(array, function (values) {
models.Goods.find({
where: {
id: values.id
}
}).then(function (res) {
if (!res || res.length === 0) {
throw new Error("Item not found");
}
});
}).then(function (res) {
//do something after check
//next step
}).catch(function(error) {
console.log(error.message);
});
I need to check if the id is in my database or not before go the next step but in this code if there is any error the throw new Error("Item not found");
never go to the catch function
so I try to do something to get the error function. I changed the code inside the promise.map
, I put catch function in models.Goods
and console.log
the error, the error shows up but the promise.map
still running and go to the //next step
section and not stop.
Please help me how to break the promise.map
if there is an error in models.Goods
Thank you