Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
122 views
in Technique[技术] by (71.8m points)

javascript - Conditional envelope in multiple function in JS

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...