MEAN Stack:无法读取未定义的属性“ companyTitle”

时间:2018-06-21 05:45:46

标签: javascript node.js angular express mean-stack

我在MongoDB数据库中创建了一个名为joblist的集合。我还创建了一个名为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');

这是我正在使用路线的路线文件夹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 cmd手动在数据库中插入了一些数据。我可以通过

GET方法来获取它们
  

http://localhost:3000/api/appliedjobs

但是当我尝试使用邮递员post进行一些数据操作时,我得到了如下错误 发布

  

TypeError:无法读取未定义的属性“ companyTitle”       在D:\ product \ project-1 \ routes \ users.js:115:28       在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)

我不知道我的代码有什么问题。有人可以帮忙吗?我想获取数据并将其发布到名为joblist的集合中。

2 个答案:

答案 0 :(得分:1)

请测试要求的正文

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));

})

和邮递员套装

  

“ Content-Type” =“ application / json”

并在正文中选择类型

  

“原始”和“ JSON(应用程序/ json)”

这应该有效

答案 1 :(得分:0)

您已经安装了正文解析器吗?

这曾经是express的一部分,但现在您必须单独安装。

所以先安装它:

npm install --save body-parser

然后要求它:

var bodyParser = require('body-parser');
var app = express();

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));