我的帖子请求并没有将我的所有数据保存在我的架构中

时间:2017-07-28 02:18:15

标签: node.js mongodb

我现在面临的问题是我的帖子请求,假设将我的文档保存在我的mongodb中并不能保存我的所有文档。它不会使用多个值保存字段。有没有办法保存这些?

这是我的帖子请求。

router.post('/register',function(req,res,next){
    let newDoc = new Doc({
        patientInfo:{
            Name:{      
                fname:req.body.fname,
                lname:req.body.lname,
                mname:req.body.mname
            },
        age:req.body.age,
        address:{
            barangay:req.body.barangay,
            cc:req.body.cc,
            city:req.body.city
        },
        birthday:req.body.birtday,
        complaint:req.body.complaint,
        vitalsign:{
            time:req.body.time,
            pulse:req.body.pulse,
            bp:req.body.bp,
            resp:req.body.resp,
            temp:req.body.temp,
            Sao:req.body.Sao,
            levelofconsciousness:req.body.levelofconsciousness,
            airway:req.body.airway,
            o2supply:req.body.o2supply,
            breathing:req.body.breathing,
            capref:req.body.capref,
            bloodsugar:req.body.bloodsugar,
            anatomicalchart:req.body.anatomicalchart
        }
        },
    managementnotes:req.body.managementnotes,
    date:{
        month:req.body.month,
        dateN:req.body.dateN,
        year:req.body.year
    },
    nameofcaller:req.body.nameofcaller,
    teamresponder:req.body.teamresponder,
    emergencyroutetimerecord:{
        callrecieved:req.body.callrecieved,
        respondtime:req.body.respondtime,
        arrivaltoscene:req.body.arrivaltoscene,
        leftscene:req.body.leftscene,
        hospitalarrival:req.body.hospitalarrival,
        acdrrmoarrival:req.body.acdrrmoarrival
    },
    natureaccident:[
        req.body.natureaccident
    ],
    vehicleinvolved:req.body.vehicleinvolved,
    incidentlocation:req.body.incidentlocation
    });
    Doc.addDocument(newDoc,function(err,callback){
        console.log(newDoc);
        if(err) res.json({success:false,msg:"ERROR SAVING DOC OCCURED"});
        else res.json({success:true,msg:"Saved"});
    });
});

它给了我这个json

{ __v: 0,
  managementnotes: 'req.body.managementnotes',
  nameofcaller: 'req.body.nameofcaller',
  teamresponder: 'req.body.teamresponder',
  vehicleinvolved: 'req.body.vehicleinvolved',
  incidentlocation: 'req.body.incidentlocation',
  _id: 597b6f1806cb8e2710e5d8f9,
  natureaccident: [ 'req.body.natureaccident' ] }

这就是我保存文档的方式

module.exports.addDocument = function(newDoc,callback){
    newDoc.save(callback);
};

知道我做错了什么吗?这是我的mongoose架构

var DocSchema = mongoose.Schema({
    patientInfo:{
        Name:{
            fname:{
                type:String
            },
            mname:{
                type:String
            },
            fname:{
                type:String
            }
        },
        age:{
            type:Number
        },
        address:{
            barangay:{
                type:String
            },
            cc:{//country
                type:String
            }
        },
        birthday:{
            type:String
        },
        complaint:{
            type:String
        },
        vitalsign:{
            time:{
                type:String
            },
            pulse:{
                type:String
            },
            bp:{
                type:String
            },
            resp:{
                type:String
            },
            temp:{
                type:String
            },
            Sao:{
                type:String
            },
            levelofconsciousness:{
                type:String
            },
            airway:{
                type:String
            },
            o2supply:{
                type:String
            },
            breathing:{
                type:String
            },
            capref:{
                type:String
            },
            bloodsugar:{
                type:String
            },
            anatoymicalchart:{
                type:String
            }
        }

    },
    managementnotes:{
        type:String
    },
    date:{
        month:{
            type:String
        },
        dateN:{
            type:String
        },
        year:{
            type:String
        }
    },
    nameofcaller:{
        type:String
    },
    teamresponder:{
        type:String
    },
    emergencyroutetimerecord:{
        callrecieved:{
            type:String
        },
        respondtime:{
            type:String
        },
        arrivaltoscene:{
            type:String
        },
        leftscene:{
            type:String
        },
        hospitalarrival:{
            type:String
        },
        acdrrmoarrival:{
            type:String
        }
    },
    natureaccident:[{
        type:String
    }],
    vehicleinvolved:{
        type:String
    },
    incidentlocation:{
        type:String
    }

});

这就是我的要求

{
"patientInfo":{
            "Name":{      
                "fname":"tslph",
                "lname":"sdfsdf",
                "mname":"sdfsdfsd"
            },
        "age":12,
        "address":{
            "barangay":"req.body.barangay",
            "cc":"req.body.cc",
            "city":"req.body.city"
        },
        "birthday":"req.body.birtday",
        "complaint":"req.body.complaint",
        "vitalsign":{
            "time":"req.body.time",
            "pulse":"req.body.pulse",
            "bp":"req.body.bp",
            "resp":"req.body.resp",
            "temp":"req.body.temp",
            "Sao":"req.body.Sao",
            "levelofconsciousness":"req.body.levelofconsciousness",
            "airway":"req.body.airway",
            "o2supply":"req.body.o2supply",
            "breathing":"req.body.breathing",
            "capref":"req.body.capref",
            "bloodsugar":"req.body.bloodsugar",
            "anatomicalchart":"req.body.anatomicalchart"
        }
        },
    "managementnotes":"req.body.managementnotes",
    "date":{
        "month":"req.body.month",
        "dateN":"req.body.dateN",
        "year":"req.body.year"
    },
    "nameofcaller":"req.body.nameofcaller",
    "teamresponder":"req.body.teamresponder",
    "emergencyroutetimerecord":{
        "callrecieved":"req.body.callrecieved",
        "respondtime":"req.body.respondtime",
        "arrivaltoscene":"req.body.arrivaltoscene",
        "leftscene":"req.body.leftscene",
        "hospitalarrival":"req.body.hospitalarrival",
        "acdrrmoarrival":"req.body.acdrrmoarrival"
    },
    "natureaccident":[
        "req.body.natureaccident"
    ],
    "vehicleinvolved":"req.body.vehicleinvolved",
    "incidentlocation":"req.body.incidentlocation"
}

1 个答案:

答案 0 :(得分:0)

在您的控制器中,您正在从请求中错误地读取参数。您需要指定对象内部值的完整路径,如下所示:

 let newDoc = new Doc({
        patientInfo:{
            Name:{      
                fname:req.body.patientInfo.Name.fname,
                lname:req.body.patientInfo.Name.lname,
                mname:req.body.patientInfo.Name.mname
            },
        age:req.body.age,
        address:{
            barangay:req.body.patientInfo.address.barangay,
            cc:req.body.patientInfo.address.cc,
            city:req.body.patientInfo.address.city
        },
        birthday:req.body.patientInfo.birtday,
        complaint:req.body.patientInfo.complaint,
        vitalsign:{
            time:req.body.patientInfo.vitalsign.time,
            pulse:req.body.patientInfo.vitalsign.pulse,
            bp:req.body.patientInfo.vitalsign.bp,
            resp:req.body.patientInfo.vitalsign.resp,
            temp:req.body.patientInfo.vitalsign.temp,
            Sao:req.body.patientInfo.vitalsign.Sao,
            levelofconsciousness:req.body.patientInfo.vitalsign.levelofconsciousness,
            airway:req.body.patientInfo.vitalsign.airway,
            o2supply:req.body.patientInfo.vitalsign.o2supply,
            breathing:req.body.patientInfo.vitalsign.breathing,
            capref:req.body.patientInfo.vitalsign.capref,
            bloodsugar:req.body.patientInfo.vitalsign.bloodsugar,
            anatomicalchart:req.body.patientInfo.vitalsign.anatomicalchart
        }
        },
    managementnotes:req.body.managementnotes,
    date:{
        month:req.body.date.month,
        dateN:req.body.date.dateN,
        year:req.body.date.year
    },
    nameofcaller:req.body.nameofcaller,
    teamresponder:req.body.teamresponder,
    emergencyroutetimerecord:{
        callrecieved:req.body.emergencyroutetimerecordcallrecieved,
        respondtime:req.body.emergencyroutetimerecord.respondtime,
        arrivaltoscene:req.body.emergencyroutetimerecord.arrivaltoscene,
        leftscene:req.body.emergencyroutetimerecord.leftscene,
        hospitalarrival:req.body.emergencyroutetimerecord.hospitalarrival,
        acdrrmoarrival:req.body.emergencyroutetimerecord.acdrrmoarrival
    },
    natureaccident:[
        req.body.natureaccident
    ],
    vehicleinvolved:req.body.vehicleinvolved,
    incidentlocation:req.body.incidentlocation
    });
    Doc.addDocument(newDoc,function(err,callback){
        console.log(newDoc);
        if(err) res.json({success:false,msg:"ERROR SAVING DOC OCCURED"});
        else res.json({success:true,msg:"Saved"});
    });