javascript - Iterate on req.body.objectName to test whether the key-value pairs are valid or not in node js -
the problem statement : "the node js webservice server application should first validate whether req.body.object contains valid key-value pairs, if present go mongodb specific method , crud operation , send response otherwise send response key-value pairs not correct or similar."
so, achieving this, tried putting things below 1 @ route:
if ((updatevalues.configid == null) || (updatevalues.configname == null) || (updatevalues.description == null) || (updatevalues.version == null) || (updatevalues.classname == null) || (updatevalues.configgroups_info.allevents == null) || (updatevalues.configgroups_info.billingdatecleard == null) || (updatevalues.configgroups_info.billingscheduleexpiration == null) || (updatevalues.configgroups_info.configurationerrordetected == null) || (updatevalues.configgroups_info.dailyselfreadtime == null) || (updatevalues.configgroups_info.datavinehypersproutchange == null) || (updatevalues.configgroups_info.datavinesyncfatherchange == null) || (updatevalues.configgroups_info.demand == null) || (updatevalues.configgroups_info.demandresetoccured == null) || (updatevalues.configgroups_info.deregistrationresult == null) || (updatevalues.configgroups_info.enablevoltagemonitor == null) || (updatevalues.configgroups_info.energy1 == null) || (updatevalues.configgroups_info.highvoltagethresholddeviation == null) || (updatevalues.configgroups_info.historylogcleared == null) || (updatevalues.configgroups_info.interrogationsendsucceeded == null) || (updatevalues.configgroups_info.intervallength == null) || (updatevalues.configgroups_info.linkfailure == null) || (updatevalues.configgroups_info.linkmetric == null) || (updatevalues.configgroups_info.loadprofileerror == null) || (updatevalues.configgroups_info.lowbatterydetected == null) || (updatevalues.configgroups_info.lowvoltagethreshold == null) || (updatevalues.configgroups_info.lowvoltagethresholddeviation == null) || (updatevalues.configgroups_info.outagelength == null) || (updatevalues.configgroups_info.primarypowerdown == null) || (updatevalues.configgroups_info.pulseweight1 == null) || (updatevalues.configgroups_info.pulseweight2 == null) || (updatevalues.configgroups_info.pulseweight3 == null) || (updatevalues.configgroups_info.pulseweight4 == null) || (updatevalues.configgroups_info.quantity4 == null) || (updatevalues.configgroups_info.rmsvolthighthreshold == null) || (updatevalues.configgroups_info.rmsvoltloadthreshold == null) || (updatevalues.configgroups_info.receivedmessagefrom == null) || (updatevalues.configgroups_info.sendresponsefailed == null) || (updatevalues.configgroups_info.tablesendrequestfailed == null) || (updatevalues.configgroups_info.testmodedemandintervallength == null) || (updatevalues.configgroups_info.timetoremainintestmode == null) || (updatevalues.configgroups_info.zigbeesetunnelingmessage == null) || (updatevalues.configgroups_info.zigbeesimplemeteringmessage == null)) { res.json({ "type": false, "status": "invalid parameter" }); } else { dbcmd.updateconfigdatavalues(updatevalues, function (err, data) { if (err) { res.json({ "type": false, "message": err.message, }); } else { res.json({ "type": true, "message": data.tostring(), }); } }); }
});
module.exports = router;
what feel not right or optimized way achieving above said objective. i want compact/compress if condition code better. kindly help.
you can put fields in array that:
let fields = [ 'configid', 'configname', 'configgroups_info.allevents' ...]; if (_.some(fields, f => _.get(updatevalues, f) === null)) { res.json({ "type": false, "status": "invalid parameter" }); } else ...
btw, want make sure fields present, checking if null not correct. if field missing, undefined
not null
. can check !_.get(updatevalues, f)
. true null, undefined, 0, empty string , false. if need check need specifically, null , undefined.
Comments
Post a Comment