0

I am new to mongoose and I was trying a few basic things with express. The data isn't being retrieved in the format of the schema. It includes the id and another value not the first name or the last name. The post seems to work fine. Can anyone help me out in this?

var entrySchema = new Schema({
  firstname: String,
  lastname: String
});

var Entry = mongoose.model('Entry', entrySchema);

/* GET home page. */
router.get("/",function(req,res,next){

    Entry.find(function(err,entries){   
        if(err)
            {
                res.send(err);
            }

        res.send(entries);
    })


})

router.post("/",function(req,res){
    var entry = new Entry();
    entry.firstname = req.body.firstname;
    entry.lastname = req.body.lastname;

    entry.save(function(err){
        if(err){
            res.send(err);
        }
        res.send({message: "success"});
    });
})
Kamagatos
  • 854
  • 9
  • 12
user3428217
  • 355
  • 2
  • 5

2 Answers2

0

I believe you missed to define Schema as mongoose.Schema like -

var Schema = mongoose.Schema;
var entrySchema = new Schema({
  firstname: String,
  lastname: String
});
swapnesh
  • 26,318
  • 22
  • 94
  • 126
  • i have the first line in the code. Sorry i missed to add it. it isn't working with that – user3428217 May 07 '15 at 14:53
  • @user3428217 can you console `req.body.firstname` and check whether its coming or not ? – swapnesh May 07 '15 at 14:55
  • they both are undefined and i checked the below URL and changed accordingly. http://stackoverflow.com/questions/24330014/bodyparser-is-deprecated-express-4 But still the vars are undefined – user3428217 May 07 '15 at 15:14
  • @user3428217 Check the content-type..try by setting headers as `Content-Type: application/json` while hitting the api..and let me know then. – swapnesh May 07 '15 at 15:20
0

Solved the issue. The problem was solved with multer node dependency as suggested in https://github.com/expressjs/multer

user3428217
  • 355
  • 2
  • 5