简体   繁体   中英

Multiple mongoose queries

I'm using node and mongoose to run queries against my mongodb. I have 3 queries that I'm running as follows :

router.put('/user/reset/password/:token',(request,response)=>{
let email = "";
if(!request.params.token){
    return response.status(400).send("Request Parameter Is Missing")
}

if(!request.body){
    return response.status(400).send("Request Body Is Missing")
}

AuthModel.findOne({
    token:request.params.token,
}).then(document =>{
    if(document === null){
        return response.status(401).send("User Naot Found")
    }
    email = document.email;

    let userModel ={
        email:email,
        password:request.body.password
    }

    UserModel.findOneAndUpdate({
        email:email,
    },userModel,{
        new:true
    }).then(document =>{
        if(document === null){
            return response.status(500).send("User not found")
        }
        AuthModel.findOneAndRemove({
            email:email,
        }).then(document =>{
            if(document === null){
                return response.status(500).send("User not found")
            }
            return response.status(200).send(document)
        }).catch(error =>{
            return response.status(500).json(error)
        })
        return response.status(200).send(document)
    }).catch(error =>{
        return response.status(500).json(error)
    })
}) .catch(error =>{
    response.status(500).send(error)
});

});

I feel like my current approach is rather inefficient and was wondering if there are efficient ways to achieve this.

Of course, this can be simplified and make more readable with async/await: try this out:

router.put('/user/reset/password/:token', async (request,response) => {
    try {
        if(!request.params.token) return response.status(400).send("Request Parameter Is Missing");
        if(!request.body) return response.status(400).send("Request Body Is Missing");
        const document = await AuthModel.findOne({token:request.params.token,});
        if(!document) response.status(401).send("User Naot Found");
        const { email } = document;
        const userDocument = {
            email,
            password: request.body.password,
        };
        const updateRes = await UserModel.findOneAndUpdate({ email, }, userDocument, { new: true });
        if(!updateRes) return response.status(401).send("User not found");
        const removeRes = await AuthModel.findOneAndRemove({ email, });
        if(!removeRes) return response.status(401).send("User not found");
        return response.status(200).send(document);
    } catch(error) {
        return response.status(500).send(error);
    }
});

ps: you may need some modifications as i couldn't get a chance to try the code. hope this helps :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM