Hamza Haddad
Hamza Haddad

Reputation: 1556

Mongodb not casting data correctly

When I post data on database, some data are not stored in my database .

Here's the schema of my model :

var ProjSchema = new Schema({
  leadProj: String,
  nomProj: String,
  descProj: String,
  BesProj: Number,
  pers: [
    {
      name: String,
      poste: String
    }
  ],
  backlog: { fonctionnalite: [String], userStory: [String] }
});

I use Express for Api and this is the route

  .post(function(req, res) {
    var nouvProj = new NouvProj();
    nouvProj.nomProj = req.body.nomProj;
    nouvProj.leadProj = req.body.leadProj;
    nouvProj.descProj = req.body.descProj;
    nouvProj.BesProj = req.body.BesProj;
    nouvProj.pers = req.body.pers;
    nouvProj.backlog.fonctionnalite = req.body.Fonctionnalite;
    nouvProj.backlog.userStory = req.body.UserStory;
    console.log(req.body.Fonctionnalite);
    console.log(req.body.UserStory);

    // save the nouvProj and check for errors
    nouvProj.save(function(err) {
      if (err) {
        res.send(err);
        console.log("err");
      }
      res.json({
        message: "nouvProj created!"
      });
    });
  })

the output of console.log()

[ { fonctionnalite: 'oijoij' }, { fonctionnalite: 'oio' } ]
[ { userStory: 'oijoij' }, { userStory: 'poihpohi' } ]

The problem is on backlog item . I'm getting empty data in it when I get elements:

backlog : []

Update : I would precise the difference between pers and backlog Pers item is a table of {name: String, poste } backlog is table of table of {fonctionnalite: String} and {UserStory : String}

I don't understand why is that working for pers and not for backlog

Upvotes: 1

Views: 58

Answers (2)

Muhammad Soliman
Muhammad Soliman

Reputation: 23756

I think you pass only a string to your functionalite while you defined it as an array, you should push the value into your array instead of assigning a string to it.

nouvProj.backlog.fonctionnalite.push(req.body.Fonctionnalite);
nouvProj.backlog.userStory.push(req.body.UserStory);

If you want to define a flexible or loose object (schema-less), its properties or schema fields are specified at runtime, you can use Schema.Types.Mixed

//schema defintion
var proj = new Proj({
  myObject: Schema.Types.Mixed
});

//how use it 
var proj = new Proj();
proj.myObject = { any: { fields: ['test'] } }
yourSchema.save()

If you want to define your backlog as an array, I would suggest doing this.

var BackLog = new Schema({ 
 fonctionnalite: [String], 
 userStory: [String] 
};

var ProjSchema = new Schema({
  leadProj: String,
  nomProj: String,
  descProj: String,
  BesProj: Number,
  pers: [
    {
      name: String,
      poste: String
    }
  ],
  backlog: [BackLog]
});

// create a project 
var proj = new ProjSchema({
  ....
});
//you notice functionalite is an array, userStory is also an array 
proj.backlog.push({ functionalite: ['test'], userStory: ['sss'] });

proj.save(function (err) {
  ...
}

More info for Mongoose schema and modeling, can be found here or Heroku's Dev. blog.

Upvotes: 1

YouneL
YouneL

Reputation: 8351

It seems that req.body.Fonctionnalite and req.body.UserStory are both array of objects and in your shcema they are declared as array of strings, You need to redefine the ProjSchema to take over this behavior:

var ProjSchema = new Schema({
    leadProj: String,
    nomProj: String,
    descProj: String,
    BesProj: Number,
    pers: [
    {
        name: String,
        poste: String
    }
    ],
    backlog: { 
        fonctionnalite: [ { fonctionnalite: String } ], 
        userStory: [ { userStory: String } ], 
    }
});

Or you can keep ProjSchema as it is and update the front-end code in the way that fonctionnalite and userStory posted as a normal arrays:

fonctionnalite => [ 'oijoij', 'oio' ]
userStory => [ 'oijoij','poihpohi' ]

Upvotes: 1

Related Questions