向阵列添加记录时出现Mongoose Validation错误

时间:2017-08-19 05:52:19

标签: javascript node.js mongoose

我收到错误' hospital_doctor_details验证失败。路径hospitalName,路径是必需的'同时在现有医院记录中添加新的治疗细节。

由于已经创建了api中使用的医院名称的医院记录,因此Mongoose不应该抛出此错误。因为我对Mongodb相对较新,所以不知道为什么会抛出这个错误。

任何帮助确定根本原因的人都会非常感激。

    var hospitalName = req.params.hospitalname;
    var hospitalCity = req.params.hospitalcity;
    var hospitalCountry = req.params.hospitalcountry;
    var procedureName = req.body["procedureName"];
    var doctorName = req.body["doctorName"];

    var treatmentFound = false;
    var doctorFound = false;

    //create doctor promise
    const doctorPromise = new Promise((resolve, reject) =>
        counterSchema.getNext('Treatment.$.doctor.$.doctorId', collection, function (id) {
           //code 

    ));

    //create department promise
    const departmentPromise = new Promise((resolve, reject) =>
        //code

    ));

    //create procedure promise
    const procedurePromise = new Promise((resolve, reject) =>
        //code 
    ));

    //Create hospital check promise
    const dbHospitalCheckPromise = new Promise((resolve, reject) =>
       //code
    });

    //Create dbcheck promise
    const dbTreatmentCheckPromise = new Promise((resolve, reject) =>
      //code 
     })


    Promise.all([dbHospitalCheckPromise, dbTreatmentCheckPromise, departmentPromise, procedurePromise, doctorPromise])
        .then(([, searchResult , departmentID, procedureID, doctorID ]) => {

            var procedureFound = searchResult.split('|')[0];  
            procedureFound = procedureFound == "true";//convert to boolean
            var docFound = searchResult.split('|')[1];
            docFound = docFound== "true";//convert to boolean

            if (procedureFound == true && docFound == false && doctorName != null) {

                hospitalModel.findOneAndUpdate({
                    "hospitalName": hospitalName, "hospitalContact.City": hospitalCity, "hospitalContact.country": hospitalCountry, "Treatment": {
                        $elemMatch: { "name": procedureName }
                    }},
                    {
                        "$push": {                            
                            "Treatment.$.doctor": {
                                "doctorId": doctorID,
                                "doctorName": req.body["doctorName"],
                                "profilepicdir": req.body["profilePicDirectory"],
                                "medinovitadoctorRating": parseInt(req.body["medinovitaDoctorRating"]),
                                "speciality": {
                                    "specialityName": req.body["specialityName"]
                                }
                            }                           
                        }
                    },
                    { returnOriginal: false, upsert: true }, function (err, doc) {

                        if (err) {
                            logger.error("Error while updating record : - " + err.message);                            
                        } else if (doc === null) {
                            logger.error("Error while updating record : - Unable to update database");                           
                        }                       
                    }).
        then(function () {
            hospitalSchema.save(function (error, data) {
                if (error) {
                    console.log("Save failed")
                    logger.error("Error while inserting record : - " + error.message);
                    return res.json({ "Message": error.message });
                }
                else {
                    console.log("saved")
                    return res.json({ "Message": "Data got inserted successfully" });
                }
            });
        })
        .catch(function (err) {
            return res.json({ "Message": err.message });
        });
}  

我的架构如下所示,

hospitalID: 10001,
hospitalName: 'Renai MediCity',
__v: 0,
updated_at: 2017-08-18T17:34:53.784Z,
Treatment:
 [ { name: 'Root Canal',
     costUpperBound: 10000,
     costLowerBound: 8000,
     departmentId: 10001,
     procedureid: 10001,
     departmentName: 'Dental',
     _id: 599725530c126c1efc43dc52,
     doctor:[ {
            profilepicdir:"smdir1",
            doctorName:"Dr.vp2",
            doctorId:10002,_id:5997255c0c126c1efc43dc57

             }] },
    ],

0 个答案:

没有答案