skip to Main Content

i post data to mongodb as following:

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended:true}));
app.use('/insert',async(req,res)=>{
    const newProduct=new product({
        _id:req.body._id,
        name:req.body.name,
        description:req.body.description
    });
    try{
        await newProduct.save(); 
        //  res.json(newProduct);
        res.send(`${newProduct} inserted.`);
        //res.redirect('/');
    }
    catch(err){
        res.send(err);
    }
    });

Data is taken from the user. The user sends the data to the mango via the URL.The user enters the data into the URL as follows:

http://localhost:3000/insert?_id=23&name=GLX+2690&description=classic+phone

but receive an empty response and data do not insert to mongo. how data should be sent via URL? Is above URL wrong?

2

Answers


  1. I think it’s req.query, not req.body.

    Login or Signup to reply.
  2. 1- Try change app.use to app.post

    2- Try using this url: http://localhost:3000/insert?_id=23&name=GLX%2B2690&description=classic%2Bphone

    3- Use req.query

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search