HTTP post请求参数不会更改Mongoose架构

时间:2016-11-30 12:59:03

标签: node.js mongodb express mongoose postman

我创建了两个Mongoose模式,并希望用Postman测试它们。 当我发送一个HTTP后请求时,我的参数不会改变AnimalSchema的默认值,也不会改变StorySchema的默认值。

var mongoose = require('mongoose');
var AnimalSchema = new mongoose.Schema({
    name: {type : String, default: ""},
    audio: {type : String, default: ""},
    image: {type : String, default: ""}
});

mongoose.model('Animal', AnimalSchema);
var mongoose = require('mongoose');
var express = require('express');
var router = express.Router();

var Animal = mongoose.model('Animal');
var Story = mongoose.model('Story');

/* GET home page. */
router.get('/', function(req, res, next) {
  res.render('index', { title: 'Express' });
});

router.get('/animals', function(req, res, next){
  Animal.find(function(err, animals){
    if(err){
      return next(err);
    }
    res.json(animals);
  });
});

router.post('/animals', function(req, res, next){
  var animal = new Animal(req.body);

  animal.save(function(err,animal){
    if(err){return next(err);}

    res.json(animal);
  });
});

router.get('/stories', function(req, res, next){
  Story.find(function(err, stories){
    if(err){return next(err);}

    res.json(stories);
  });
});

router.post('/stories', function(req, res, next){
  var story = new Story(req.body);

  story.save(function(err, story){
    if(err){return next(err);}

    res.json(story)
  });
});

module.exports = router;

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 mongoose = require('mongoose');

// connect MongoDB
mongoose.connect('mongodb://localhost/stuckyToys', function(err,db){
    if (!err){
        console.log('Connected to /stuckyToys!');
    } else{
        console.dir(err); //failed to connect
    }
});

require('./models/Animals');
require('./models/Stories');

var routes = require('./routes/index');
var users = require('./routes/users');

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');

// uncomment after placing your favicon in /public
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

app.use('/', routes);
app.use('/users', users);

// 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 handler
app.use(function(err, req, res, next) {
  // set locals, only providing error in development
  res.locals.message = err.message;
  res.locals.error = req.app.get('env') === 'development' ? err : {};

  // render the error page
  res.status(err.status || 500);
  res.render('error');
});

module.exports = app;

我认为我的app.js文件出错了。但不确定它在哪里。 谁知道我做错了什么?

Database content with Robomongo

Postman 1

Postman 2

1 个答案:

答案 0 :(得分:0)

我会做像

这样的事情
n = int(raw_input("Enter a number:"))

def work(n):
    print(range(1, n+1))

work(n)