我已经创建了一个名为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;
这是我的路线文件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);
}
});
});
我遇到错误
获取工作 获得应聘职位
ReferenceError:未定义Appliedjob 在D:\ product \ project-1 \ routes \ users.js:132:3 在Layer.handle [作为handle_request](D:\ product \ project-1 \ node_modules \ express \ lib \ router \ layer.js:95:5) 在下一个(D:\ product \ project-1 \ node_modules \ express \ lib \ router \ route.js:137:13) 在Route.dispatch(D:\ product \ project-1 \ node_modules \ express \ lib \ router \ route.js:112:3) 在Layer.handle [作为handle_request](D:\ product \ project-1 \ node_modules \ express \ lib \ router \ layer.js:95:5) 在D:\ product \ project-1 \ node_modules \ express \ lib \ router \ index.js:281:22 在Function.process_params(D:\ product \ project-1 \ node_modules \ express \ lib \ router \ index.js:335:12) 在下一个(D:\ product \ project-1 \ node_modules \ express \ lib \ router \ index.js:275:10) 在D:\ product \ project-1 \ routes \ users.js:15:3 在Layer.handle [作为handle_request](D:\ product \ project-1 \ node_modules \ express \ lib \ router \ layer.js:95:5) 在trim_prefix(D:\ product \ project-1 \ node_modules \ express \ lib \ router \ index.js:317:13) 在D:\ product \ project-1 \ node_modules \ express \ lib \ router \ index.js:284:7 在Function.process_params(D:\ product \ project-1 \ node_modules \ express \ lib \ router \ index.js:335:12) 在下一个(D:\ product \ project-1 \ node_modules \ express \ lib \ router \ index.js:275:10) 在Function.handle(D:\ product \ project-1 \ node_modules \ express \ lib \ router \ index.js:174:3) 在路由器(D:\ product \ project-1 \ node_modules \ express \ lib \ router \ index.js:47:12)
我已在数据库(MongoDB)中签入,尚未生成集合。
答案 0 :(得分:0)
在创建模型时将其放在行下
var joblist = mongoose.model('jobList', jobListSchema, 'jobList');
module.exports = joblist;
答案 1 :(得分:0)
appliedjob
作业是在router.post
到/appliedjobs
的回调中定义的,并且在函数完成后即消失,因此它router.get
中不再存在
如果要使其保留,则需要在函数外声明appliedjob
。这是新的user.js文件:
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);
}
});
});
这样,appliedjob
只会是您期望的,如果在post
请求之前发生了get
请求
我建议您重新考虑该策略,这样,如果首先发生get
请求,您就可以恢复。
答案 2 :(得分:0)
在函数范围之外声明变量Appliedjob
答案 3 :(得分:0)
您应该修改Jobslist.js文件的最后一行。
以下是猫鼬模型的正确导出。
module.exports = { jobList : mongoose.model('jobList', jobListSchema) };
在导入时更改第一行
var JobList = require('../models/jobslist').jobList;
答案 4 :(得分:0)
应使用模式jobList
而不是模式实例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);
}
});
});