0
votes

I have created a collection in my MongoDB database called joblist. I have also created a schema of DB called jobList.js

var mongoose = require('mongoose');

const joblistSchema = mongoose.Schema({
    companyTitle: String,
    jobTitle: String,
    location: String,
});

const JlSchema = module.exports = mongoose.model('JlSchema',joblistSchema,'joblist');

This is route folder users.js where I'm using my routes

const jobList = require('../models/jobList');
//post joblist
router.post('/appliedjobs', function(req,res) {
  console.log('posting');
  jobList.create({
    companyTitle: req.body.companyTitle,
    jobTitle: req.body.jobTitle,
    location: req.body.location
  },function(err,list) {
    if (err) {
      console.log('err getting list '+ err);
    } else {
      res.json(list);
    }
  }
  );
});
//getting joblistlist
router.get('/appliedjobs',function(req,res) {
  console.log('getting list');
  jobList.find(function(err,list) {
    if(err) {
      res.send(err);
    } else {
      res.json(list);
    }
  });
});

I have inserted some data in database manually(by using mongodb cmd).I can get them by GET method from

http://localhost:3000/api/appliedjobs

but when I'm trying to post some data using postman I'm getting error as posting

TypeError: Cannot read property 'companyTitle' of undefined at D:\product\project-1\routes\users.js:115:28 at Layer.handle [as handle_request] (D:\product\project-1\node_modules\express\lib\router\layer.js:95:5) at next (D:\product\project-1\node_modules\express\lib\router\route.js:137:13) at Route.dispatch (D:\product\project-1\node_modules\express\lib\router\route.js:112:3) at Layer.handle [as handle_request] (D:\product\project-1\node_modules\express\lib\router\layer.js:95:5) at D:\product\project-1\node_modules\express\lib\router\index.js:281:22 at Function.process_params (D:\product\project-1\node_modules\express\lib\router\index.js:335:12) at next (D:\product\project-1\node_modules\express\lib\router\index.js:275:10) at D:\product\project-1\routes\users.js:15:3 at Layer.handle [as handle_request] (D:\product\project-1\node_modules\express\lib\router\layer.js:95:5) at trim_prefix (D:\product\project-1\node_modules\express\lib\router\index.js:317:13) at D:\product\project-1\node_modules\express\lib\router\index.js:284:7 at Function.process_params (D:\product\project-1\node_modules\express\lib\router\index.js:335:12) at next (D:\product\project-1\node_modules\express\lib\router\index.js:275:10) at Function.handle (D:\product\project-1\node_modules\express\lib\router\index.js:174:3) at router (D:\product\project-1\node_modules\express\lib\router\index.js:47:12)

I don't know what's wrong in my code. can anyone help? I want get and post data to collection called joblist.

2
Cannot read property 'companyType' of undefined I don't see companyType anywhere but the title?CertainPerformance
body in req.body is undefinedAdelin
Possible duplicate of Express.js req.body undefinedAdelin
ya I got it I have just kept the app.use(bodyParser.urlencoded({ extended: false })); app.use(bodyParser.json()); before the routes.user9705886
I don't know what is "router", but I'am sure you forget a return in anywhereEliseo

2 Answers

1
votes

Please test the req.body

var bodyParser = require('body-parser');
var app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: false}));

Then in your router

router.post('/appliedjobs', function(req,res) {
      console.log(JSON.parse(req.body));

})

and in postman set

"Content-Type"="application/json"

and in body select the type as

"raw" and "JSON(application/json)"

This should work

0
votes

Have you installed the body parser?

This used to be a part of express but now you have to install it separately.

So first install it:

npm install --save body-parser

Then require it:

var bodyParser = require('body-parser');
var app = express();

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));