0

I am able to store data in my mongoDB database using req.body in a POST request but I am not able to retrieve that data using req.body in a GET request. I can retrieve data if I pass a string but not req.body.. I have seen there are multiple posts on this issue in which body parser seems to solve the issue but I am still retrieving undefined even though I have body parser going.

Thanks in advance.

const express = require('express');
const app = express();
const mongoose = require('mongoose'); 
const path = require('path'); 
const bodyParser = require('body-parser'); 

const PORT = process.env.PORT || 2000;

// app.use(express.urlencoded({extended: true}));
app.use(bodyParser.urlencoded({ extended: false })); 
// app.use(express.json());
app.use(bodyParser.json()); 
app.use(express.static('public')); 


const database = mongoose.connect('mongodb://localhost/users')
.then(() => console.log('connected to mongoDB!!'))
.catch((err) => console.error('unable to connect', err)); 

const userSchema = {
    name: String,
    email: String
}
const User = mongoose.model('User', userSchema);

app.post('/api/users',(req,res) => {
    const user = new User({
        name: req.body.name,            
        email: req.body.email
    });
    const create = user.save(); 
    res.send('created a user account'); 
});

app.get('/api/users', async (req,res) => {
    const find = await User
    .find({ name: req.body.name});

    console.log(find); 
    res.send(`your data is ${req.body.name}`); 
})

app.listen(PORT, () => console.log(`listening on port ${PORT}`)); 
Dave
  • 1
  • 2
  • Possible duplicate of [Parse body from GET request using nodejs, express, body-parser?](https://stackoverflow.com/questions/36425159/parse-body-from-get-request-using-nodejs-express-body-parser) – wrobbinz Aug 08 '19 at 22:07
  • GET request never have body in it. You can use it with req.query or req.params. req.params contains route parameters (in the path portion of the URL), and req.query contains the URL query parameters (after the ? in the URL). – Jaspreet kaur Aug 09 '19 at 04:29
  • Thank you Jaspreet. – Dave Aug 09 '19 at 17:36

2 Answers2

2

GET Resquest doesn't have a body. Instead you need to use query parameters.

You can do something like:

app.get('/api/users', async (req,res) => {
  const find = await User
  .find({ name: req.query.name});

  console.log(find); 
  res.send(`your data is ${req.query.name}`); 
})
Leandro Lima
  • 121
  • 1
  • 8
1

To complement Leandro Lima's answer, for route parameters:

app.get('/api/users/:name', async (req,res) => {
    const find = await User
    .find({ name: req.params.name});

    console.log(find); 
    res.send(`your data is ${req.params.name}`); 
})

The .../:name part makes up req.params.name (for example, .../:id then is req.params.id).

And for query parameters: /api/users/Dave?sex=male&developer=yes
Then req.queryhas {sex: 'male', developer: 'yes'}, thus could use req.query.sex and req.query.developer as you see fit.

Check out this question too: Node.js: Difference between req.query[] and req.params

Matin Sasan
  • 1,835
  • 1
  • 13
  • 26