2
votes

I've created a schema called jobslist.js

 var mongoose = require('mongoose');
var Schema = mongoose.Schema;


var jobListSchema = new Schema({
    companyName: String,
    jobtitle: String,
    location: String
});

var joblist = mongoose.model('jobList', jobListSchema);

module.exports = joblist;

this is my routes file user.js

    const jobList = require('../models/joblist');
        router.post('/appliedjobs', function(req,res) {
  console.log('posting applied jobs list');
  var appliedjob = new jobList();
      appliedjob.companyName = req.body.companyName;
      appliedjob.jobtitle =  req.body.jobtitle;
      appliedjob.location = req.body.location;
  console.log(appliedjob);
  appliedjob.save(function(err,joblist) {
    if(err) {
      console.log(err);
    } else {
      res.json({msg: 'job is saved'});
    }
  });
});

//get applied job list
router.get('/appliedjobs',function(req,res ) {
  console.log('getting applied jobs');
  appliedjob.find(function(err, appjobs) {
    if (err) {
      console.log('unable to get jobs '+ err);
    } else {
      console.log('getting list');
      res.json(appjobs);
    }
  });
});

I'm getting error as

fetching jobs getting applied jobs

ReferenceError: appliedjob is not defined at D:\product\project-1\routes\users.js:132:3 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 have checked in my database(MongoDB), collection has been not generated.

5
You are also mixing ES2015 and ES5. Declare all var -> const or don't use const - carkod

5 Answers

1
votes

You should modify the last line of jobslist.js file.

Following is the proper exporting of mongoose model.

module.exports = { jobList : mongoose.model('jobList', jobListSchema) };

And while importing it change the first line

var JobList = require('../models/jobslist').jobList;
0
votes

Put below line when you create a model

var joblist = mongoose.model('jobList', jobListSchema, 'jobList');

module.exports = joblist;
0
votes

Declare variable appliedjob outside the function scope

0
votes

appliedjob jobs is defined during router.post to /appliedjobs, in its callback and dies as soon as the function finishes, so it no longer exists in router.get

If you want it to persist, you need to declare appliedjob outside the function. Here is the new user.js file:

const jobList = require('../models/joblist');
let appliedjob; // now it's available to both functions below
//post joblist
router.post('/appliedjobs', function(req,res) {
  console.log('posting applied jobs list');
  appliedjob = new jobList({
     companyName : req.body.companyName,
     jobtitle :  req.body.jobtitle,
     location : req.body.location
  });
  console.log(appliedjob);
  appliedjob.save(function(err,joblist) {
    if(err) {
      console.log(err);
    } else {
      res.json({msg: 'job is saved'});
    }
  });
});

//get applied job list
router.get('/appliedjobs',function(req,res ) {
  console.log('getting applied jobs');
  appliedjob.find(function(err, appjobs) {
    if (err) {
      console.log('unable to get jobs '+ err);
    } else {
      console.log('getting list');
      res.json(appjobs);
    }
  });
});

But!

This way appliedjob will only be what you expect it to be if a post request happened before the get request.

I suggest you rethink the strategy a bit, so that if a get request happens first, you can recover.

0
votes

should use schema jobList instead of schema instance appliedjob.

//get applied job list
router.get('/appliedjobs',function(req,res ) {
  console.log('getting applied jobs');

    jobList.find(function(err, appjobs) {     //find by jobList schema
 // appliedjob.find(function(err, appjobs) { 

    if (err) {
      console.log('unable to get jobs '+ err);
    } else {
      console.log('getting list');
      res.json(appjobs);
    }
  });
});