2016-09-21 21 views
1

我在MongoDB中轉換成數組失敗,moongoose和字典

這裏有貓鼬插入數據的問題是我的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腳本測試:

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

}); 
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快遞進入點

以下是錯誤:

{ [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,但我不明白爲什麼,如果任何人有任何線索。

感謝和問候

回答

1

你的Python腳本只發送字典的鍵爲fiters,嘗試添加.items()派2元組。我不確定您的ORM期望的格式。

如果不工作,JSON也可用於通過POST傳遞複雜的結構。

+0

我做過嘗試,但它並沒有奏效:/我將與JSON感謝嘗試 – user462794

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)