【问题标题】:Empty objects using app.post and AngularJS + mongoose on node.js在 node.js 上使用 app.post 和 AngularJS + mongoose 清空对象
【发布时间】:2014-12-07 02:58:07
【问题描述】:

我的 REST API 正在发布空对象。

我从 req.body.name

获得价值

如果我记录它 console.log(req.body.name); 我会在控制台上获得值。

POST: 
{ name: 'typing any name', status: null }
typing any name

所以我的前端 (angular.js)、表单和后端 (node.js、express、mongoose) 之间的工作流程似乎有效。现在我发布了这个值,但我在我的 mongoDB 中得到了一个空对象。

{"_id":"543a50a974de6e2606bd8478","__v":0}

app.post('/api/offers', function (req, res){
var offer;
console.log("POST: ");
console.log(req.body);
console.log(req.body.name);
offer = new OfferModel({
 name: req.body.name,
 value: req.body.value,
 title: req.body.title,
 content: req.body.content,
});
offer.save(function (err) {
 if (!err) {
    return console.log("created offer" + req.body.name);
  } else {
    return console.log(err);
  }
});
 return res.send(offer);
});

这是模型:

var offerSchema = mongoose.Schema({

offer            : {
        name        : String,
        value       : String,
        title       : String,
        content     : String,
        image       : String,
        start       : String,
        end         : String,
        targets     : String,
        beacons     : String,
        published   : String
}
});
var OfferModel = mongoose.model('Offer', offerSchema);

【问题讨论】:

    标签: node.js angularjs mongodb express mongoose


    【解决方案1】:

    Schema 不正确,应该是这样的:

    var offerSchema = mongoose.Schema({
        name        : String,
        value       : String,
        title       : String,
        content     : String,
        image       : String,
        start       : String,
        end         : String,
        targets     : String,
        beacons     : String,
        published   : String
    });
    

    【讨论】:

      猜你喜欢
      • 2013-12-27
      • 2021-01-22
      • 2019-04-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-06
      • 1970-01-01
      • 2021-08-16
      相关资源
      最近更新 更多