我在我的MongoDB数据库中创建了一个名为joblist的集合。我还创建了一个名为jobList.js的DB模式。
var mongoose = require('mongoose');
const joblistSchema = mongoose.Schema({
companyTitle: String,
jobTitle: String,
location: String,
});
const JlSchema = module.exports = mongoose.model('JlSchema',joblistSchema,'joblist');这是路由文件夹users.js,我在这里使用我的路由
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);
}
});
});我手动在数据库中插入了一些数据(使用mongodb ),.I可以通过GET方法从
http://localhost:3000/api/appliedjobs
但是,当我试图使用邮递员post一些数据时,我得到的错误是发帖
(D:\product\project-1\node_modules\express\lib\router\route.js:137:13):无法读取未定义的属性'companyTitle‘在D:\product\project-1\routes\users.js:115:28 at next Layer.handle request at Route.dispatch (D:\product\project-1\node_modules\express\lib\router\route.js:112:3) at Layer.handle request在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::下一站为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),trim_prefix request at trim_prefixrequest(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路由器(D:\product\project-1\node_modules\express\lib\router\index.js:47:12)
我不知道我的代码有什么问题。有人能帮忙吗?我希望get和post数据收集称为joblist。
发布于 2018-06-21 07:34:44
请测试一下req.body
var bodyParser = require('body-parser');
var app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: false}));然后在你的路由器
router.post('/appliedjobs', function(req,res) {
console.log(JSON.parse(req.body));
})在邮递员组
“内容-类型”=“application/json”
在主体中选择类型为
"raw“和"JSON(application/json)”
这应该能行
发布于 2018-06-21 06:57:18
你安装了身体解析器了吗?
这曾经是快递的一部分,但现在你必须分开安装它。
所以首先安装它:
npm install --save body-parser然后要求:
var bodyParser = require('body-parser');
var app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));https://stackoverflow.com/questions/50961154
复制相似问题