Reputation:
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.
Upvotes: 2
Views: 88
Reputation: 14236
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);
}
});
});
Upvotes: 0
Reputation: 8209
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);
}
});
});
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.
Upvotes: 0
Reputation: 93
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;
Upvotes: 1
Reputation: 94
Put below line when you create a model
var joblist = mongoose.model('jobList', jobListSchema, 'jobList');
module.exports = joblist;
Upvotes: 0