0

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 Answers2

1

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

Prasobh E
  • 154
  • 1
  • 2
  • 12
0

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}));
Paul Adam
  • 251
  • 1
  • 2
  • 11
  • yes, I've installed it. but I've made extended: false –  Jun 21 '18 at 07:12
  • I see.. can I see the request body you are including in your post? Are you setting any Content-Type in the request header? – Paul Adam Jun 21 '18 at 07:26
  • addJobList(jobitem) { let headers = new Headers(); headers.append('Content-Type','application/json'); var selected = { companyTitle : jobitem.company, jobTitle : jobitem.jobtitle, location : jobitem.location } console.log(selected); this.http.post('http://localhost:3000/api/appliedjobs', JSON.stringify(selected),{headers: headers}) .map(res => res.json()); } –  Jun 21 '18 at 08:20
  • There seems to be a ";" instead of "," after the Url.. 'localhost:3000/api/appliedjobs'; <-- ... If that does not help, you can also try to send the payload data without JSON.stringify. So just this.http.post('localhost:3000/api/appliedjobs', selected, {headers: headers})... – Paul Adam Jun 21 '18 at 08:33
  • I've posted a question a question on that.https://stackoverflow.com/questions/50963671/mean-stack-post-method-is-not-reflecting-in-database –  Jun 21 '18 at 08:39