Quantcast
Channel: Active questions tagged javascript - Stack Overflow
Viewing all articles
Browse latest Browse all 141573

POST request is not responding in NodeJS

$
0
0

I have to send post request to my mongo dB server. But it takes too long time and returns timeout error message. My post method mentioned below.

const router  = require('express').Router();
const User = require('../models/User')

router.post('/register',async (req,res)=>{
    console.log(req.body)
    const user = new User({
        name: req.body.name,
        email: req.body.email,
        password: req.body.password,
    })
    try{
        const savedUser = await user.save();
        res.send("User saved");
    }catch(err){
        res.status(400).send(err)
    }
});




module.exports = router;

But when I remove promises and send request back to the server it works. can someone explain what is the issue here. My edited post method as follows. Thanks!

router.post('/register',(req,res)=>{
    console.log(req.body)
    const user = new User({
        name: req.body.name,
        email: req.body.email,
        password: req.body.password,
    })
    try{
        const savedUser = user.save();
        res.send("User saved");
    }catch(err){
        res.status(400).send(err)
    }
});

Viewing all articles
Browse latest Browse all 141573

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>