简体   繁体   中英

Iterate on req.body.objectName to test whether the key-value pairs are valid or not in node js

the problem statement is something like this : "the node js webservice server application should first validate whether the req.body.object contains valid key-value pairs, if its present then only go to mongodb specific method and do some CRUD operation and send response otherwise send response as key-value pairs are not correct or something similar."

So, for achieving this, I tried putting things like the below one at the 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 I feel is this is not the right or optimized way to achieving the above said objective. I want to compact/compress the If condition so that code could look better . kindly help.

You can put the fields in array like 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, you probably want to make sure that the fields are present, so checking if they are null is not correct. If the field is missing, it is undefined not null . You can just check that !_.get(updatevalues, f) . It will be true for null, undefined, 0, empty string and false. If it is too much you need to check what you need specifically, like null and undefined.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM