简体   繁体   English

在node.js上使用app.post和AngularJS +猫鼬清空对象

[英]Empty objects using app.post and AngularJS + mongoose on node.js

My REST API is posting empty objects. 我的REST API正在发布空对象。

I am getting the value from req.body.name 我从req.body.name获取值

If I log it console.log(req.body.name); 如果我登录它console.log(req.body.name); I get the value on my console. 我在控制台上获得了价值。

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

So the workflow between my frontend ( angular.js ), the form and the backend ( node.js, express, mongoose ) seems to work. 因此,我的前端( angular.js ),表单和后端( node.js,express, mongoose )之间的工作流程似乎正常。 Now I POST the value, but I get an empty object in my mongoDB. 现在,我发布了值,但是在mongoDB中得到了一个空对象。

{"_id":"543a50a974de6e2606bd8478","__v":0} { “_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);
});

And here is the model: 这是模型:

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

Schema is incorrect, must be like this: 模式不正确,必须是这样的:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM