It is to see how I could make a function return a value to me to be able to store it in a variable and thus use it, I want to do this for the validation of the stock, that is, if I do not have stock then I do not save anything until I remove the item or renew the stock by adding more.
async function decreaseStock(idarticle,quantity){
let {stock} = await models.Article.findOne({_id:idarticle});
if(!stock || stock < 1){
return console.log('Stock empy');
} else {
let nStock = parseInt(stock) - parseInt(quantity);
await models.Article.findByIdAndUpdate({_id:idarticle},{stock:nStock});
return console.log('Stock plus');
}
}
export default {
add: async (req,res,next) =>{
try {
//
let details = req.body.details;
details.map(function(x){
decreaseStock(x._id,x.quantity);
})
//
const reg = await models.Sales.create(req.body);
res.status(200).json(reg);
} catch (e){
res.status(500).send({
message:'An error occurred'
});
next(e);
}
},
}
Regards.
RE: Greetings, I don't actually get an error, that's the problem, but that piece of code is that I have to correct, when the add function calls the decreaseStock function that it can return a value to the add function, since they act separately, With this I try to stop the add function in case the decreaseStock function has a problem.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…