0

我正在尝试使用 mers 通过 mers 将数据存储在 mongoDB 中。这是我的设置的摘录:

文件模型/Plan.js

var mongoose = require('mongoose'),
    Schema = mongoose.Schema,
    ObjectId = mongoose.Schema.ObjectId;

var PlanSchema = new Schema({
    name: String
});

var plan = module.exports.plan = mongoose.model('plan', PlanSchema);

在 app.js 我有

"use strict";

/**
 * Module dependencies.
 */

var express = require('express'),
    routes = require('./routes'),
    mongoose = require('mongoose'),
    mers = require('mers');

var Plan = require('./models/Plan').Plan;

var app = module.exports = express.createServer();

// Configuration

app.configure(function () {
  app.set('view engine', 'ejs');
  app.set('view options', { open: '<?', close: '?>' }); // match ejs in st2
  app.set('views', __dirname + '/views');
  app.set('partials', __dirname + '/views/partials');

  app.use(express.bodyParser());
  app.use(express.methodOverride());
  app.use(express.cookieParser());
  app.use(express.session({ secret: 'your secret here' }));
  app.use(app.router);

  // Database Connection (this works)
  uri = 'mongodb://username:password@address';
  mongoose.connect(uri);

  app.use('/rest', mers({mongoose: mongoose}).rest());

  app.use(require('less-middleware')({ src: __dirname + '/public' }));
  app.use(express.static(__dirname + '/public'));
});

app.configure('development', function () {
  app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
});

app.configure('production', function () {
  app.use(express.errorHandler());
});

// Routes

app.get('/', routes.index);

app.listen(3000);
console.log("Express server listening on port %d in %s mode", app.address().port, app.settings.env);

我尝试通过调用来创建计划

curl -i -X POST -d '{"name":"foo"}' http://localhost:3000/rest/plan

结果:在 mongodb 集合“计划”中创建了一个条目,看起来像

{
    "_id": {
        "$oid": "50d0f2f2234005ef27000004"
    },
    "__v": 0
}

我缺少计划架构中定义的“名称”属性。怎么了?

4

1 回答 1

0

错误不在 JavaScript 代码中,而是 curl 请求:

curl -X POST -H "Content-Type: application/json" -d '{"name":"foo"}' http://localhost:3000/rest/Plan

感谢https://stackoverflow.com/a/1836922/449436

于 2012-12-20T06:34:19.613 回答