使用moongoose和dict,Cast to Array失败了

时间:2016-09-21 14:14:49

标签: python node.js express

我在使用mongob中的mongoose插入数据时遇到问题

这是我的db.js模型:

O(1)

这是我的python脚本测试:

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

这是我在nodejs中使用express:

的输入点
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)

这是错误:

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

});

所以这个错误似乎来自fiters dict字段,但我不明白为什么如果有人有任何线索。

谢谢和问候

2 个答案:

答案 0 :(得分:1)

您的Python脚本仅发送fiters字典的键,尝试添加.items()以发送2元组。我不确定你的ORM期望的格式。

如果这不起作用,JSON也可用于通过POST传递复杂结构。

答案 1 :(得分:1)

答案是发送json而不是数据:

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/", json=appointment,headers=headers)
print(r.status_code)
print(r.content)