Cast to Array failed with moongoose and dict
<p>I have a problem to insert a data with mongoose in mongodb </p>

<p>Here is my db.js model : </p>

<pre><code>var Appointment = new Schema({
  date: Date,
  coach: ObjectId,
      complement: String,
      isOwner: Boolean,
  fiter : ObjectId,
  fiters: [
    {
      user: ObjectId,
      isOwner: Boolean,
      status: String,
      invitationDate: Date
    }
  ],
  place: ObjectId,
  objectif : ObjectId,
  pricing: Number,
  status: String,
  ratings: [
    {
      date: Date,
      user: ObjectId,
      score: Number,
      comment: String,
      target: ObjectId,
      targetType: String
    }
  ],
  annulation : Boolean,
  late: Number,
  log: [{
    logType: String,
    date: Date,
    user: ObjectId,
    details: String,
    relatedTo: ObjectId
  }]
},
{
  timestamps: true
});
</code></pre>

<p>Here is my python script test : </p>

<pre><code>appointment = { 
   "_id":idFiter,
    "date": "2016-09-25T00:00:00.0000000Z",
    "coach":"57dfd22f7f8effc700bfa16f",
    "fiters" : [
         {
      "user": "57da891db39797707093c6e1",
      "isOwner": False,    
      "status": "invite",
      "invitationDate": "2016-09-25T00:00:00.0000000Z",
    }],
    "place" : "57d66a5b73c0ab6c007beb74",
    "objectif": "57e28b64cae2161f33b641e3",


}
r = requests.post("http://127.0.0.1:8010/appointment/", data=appointment,headers=headers)
print(r.status_code)
print(r.content)
</code></pre>

<p>and here is my enter point in nodejs with express  :</p>

<pre><code>router.post('/', authenticate.passport.authenticate('bearer', { session: false }), function(req, res) {
  appointmentToInsert =
  {
       date : req.body.date,
       coach : req.body.coach,
       fiter : req.body._id,
       fiters : req.body.fiters,
       place : req.body.place,
       objectif : req.body.objectif,
       isOwner : true,

  };
  new Appointment(appointmentToInsert).save(function (error, appointment) {
    if (error == null) {
      res.status(200).send(appointment);
    } else {
      console.log(error);
      res.status(500).send(error);
    }
  });

});
</code></pre>

<p>Here is the error :</p>

<pre><code>{ [ValidationError: Appointment validation failed]
  message: 'Appointment validation failed',
  name: 'ValidationError',
  errors: 
   { fiters: 
      { [CastError: Cast to Array failed for value "[ 'status', 'isOwner', 'invitationDate', 'user' ]" at path "fiters"]
        message: 'Cast to Array failed for value "[ \'status\', \'isOwner\', \'invitationDate\', \'user\' ]" at path "fiters"',
        name: 'CastError',
        kind: 'Array',
        value: [Object],
        path: 'fiters',
        reason: [Object] } } }
</code></pre>

<p>So the error seems to come from  the fiters dict field but i don't understand why if anyone have any clue.</p>

<p>Thanks and regards</p>

<p>Your Python script is only sending the keys of the dictionary for <code>fiters</code>, try to add <code>.items()</code> to send 2-tuples. I'm not exactly sure which format your ORM expects.</p>

<p>If that doesn't work, JSON can also be used to pass complex structures through POST.</p>

python node.js express