node.js - Nodejs - My post request doesn't save all my data in my schema -
my problem i'm facing right post request suppose save doc in mongodb doesn't save docs. doesn't save fields multiple values. there way save those?
this post request.
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"}); }); });
and returns me 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' ] }
and how save doc
module.exports.adddocument = function(newdoc,callback){ newdoc.save(callback); };
any idea i'm doing wrong? i'am new nodejs please bare me lol here mongoose schema
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 } });
this request like
{ "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" }
in controller reading params request incorrectly. need specify full path values inside objects so:
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"}); });
Comments
Post a Comment