python - Moongoose 和 dict 转换为数组失败

标签 python node.js express

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

这是我的 db.js 模型:

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

这是我的 python 脚本测试:

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)

这是我在nodejs中使用express的输入点:

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

});

这是错误:

{ [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] } } }

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

感谢和问候

最佳答案

您的Python脚本仅发送fiters的字典键,尝试添加.items()来发送2元组。我不太确定您的 ORM 需要哪种格式。

如果这不起作用,还可以使用 JSON 通过 POST 传递复杂的结构。

关于python - Moongoose 和 dict 转换为数组失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39619118/

相关文章:

javascript - 异步 setTimeout 会阻止快速处理,直到发生 res.send

javascript - socket.io 连接事件不起作用,为什么?

python - 2 列出 python 中的排列

python - 如何在不使用堆栈或队列的情况下编写自己的 Python 堆栈推送和弹出函数?

python - 如何使用 python 套接字库通过 adhoc wifi 进行广播

javascript - 通过 Node.js 进行 Ping 操作

node.js - 这个选择器没有任何属性,不会被渲染(无缘无故)

python - pandas 中可用于 to_excel 函数的引擎

node.js - 安装 PhoneGap 得到 "error not found: git"

node.js - Google Calendar API V3 不尊重 Nodejs Express 应用程序中的 timeMin timeMax 参数