我是一个完全的菜鸟,并希望开发MEAN堆栈技能。我已经有一个基本的节点设置工作,基本的GET路由工作正常,但POST和DELETE没有将数据传递给mongo。
我已经调试确定req.body未定义,但我不知道为什么 - 我已经:
检查我正在使用bodyParser 路线在配置后宣布
这是代码app.js:
var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var app = express();
var routes = require('./routes/index');
var todos = require('./routes/todos');
// set our port
var port = process.env.PORT || 3000;
//Requires the mongoose connection
var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/todoApp', function(err) {
if(err) {
console.log('connection error', err);
} else {
console.log('connection successful');
}
});
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
// uncomment after placing your favicon in /public
//app.use(favicon(__dirname + '/public/favicon.ico'));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use('/todos', todos);
app.use('/', routes);
// catch 404 and forward to error handler
app.use(function(req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
});
// error handlers
// development error handler
// will print stacktrace
if (app.get('env') === 'development') {
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: err
});
});
}
// production error handler
// no stacktraces leaked to user
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: {}
});
});
// start app ===============================================
// startup our app at http://localhost:3080
app.listen(port);
// shoutout to the user
console.log('Magic happens on port ' + port);
module.exports = app;
路线:
var express = require('express');
var router = express.Router();
var mongoose = require('mongoose');
var Todo = require('../models/Todo.js');
/* GET /todos listing. - THIS GUY IS WORKING OK */
router.get('/', function(req, res, next) {
Todo.find(function (err, todos) {
if (err) return next(err);
res.json(todos);
});
});
/* POST /todos listings */
router.post('/', function(req, res, next){
console.log(req.body); // THIS GUY IS UNDEFINED
Todo.create(req.query, function(err, post){
if (err) return next(err);
res.json(post);
});
});
module.exports = router
和架构/模型
var mongoose = require('mongoose');
var TodoSchema = new mongoose.Schema({
name: String,
completed: Boolean,
note: String,
updated_at: { type: Date, default: Date.now },
});
//note the declaration of the collection as the third argument to the model method
module.exports = mongoose.model('Todo', TodoSchema, 'todoApp');
请帮助!!
答案 0 :(得分:2)
当您对路线执行POST时,您需要将内容类型标题指定为application / json。
POST -H Content-type: 'application/json' /post/route
你用什么来测试你的帖子?