Sign up ×
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute:

My REST API is posting empty objects.

I am getting the value from req.body.name

If I log it 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. Now I POST the value, but I get an empty object in my 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);
});

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);
share|improve this question
up vote 1 down vote accepted

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
});
share|improve this answer
    
thank you! It solved the problem ... – Marcel Oct 12 '14 at 12:18
    
You are welcome )) – karaxuna Oct 12 '14 at 12:18

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.