2016-08-24 42 views
0

问题陈述如下: “节点js webservice服务器应用程序应首先验证req.body .object包含有效的键值对,如果它存在,那么只会去mongodb特定的方法并执行一些CRUD操作并发送响应,否则发送响应的键值对不正确或类似。迭代req.body.objectName以测试键值对是否在节点js中有效

那么,实现这一目标,我试图把东西像下面的一个在路线:

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; 

我觉得这不是实现上述目标的正确或优化方式。 我想压缩/压缩If条件,以便代码看起来更好。 和蔼可亲。

回答

0

你可以把字段阵列那样:

let fields = [ 
 
'ConfigID', 
 
'ConfigName', 
 
'ConfigGroups_Info.AllEvents' 
 
...]; 
 

 
if (_.some(fields, f => _.get(updatevalues, f) === null)) { 
 
res.json({ 
 
     "type": false, 
 
     "Status": "Invalid Parameter" 
 
    }); 
 
} 
 
else ...

顺便说一句,你可能想确保领域都存在,因此检查他们是否为空是不正确的。如果该字段丢失,则为undefined而不是null。你可以检查一下!_.get(updatevalues, f)。对于null,undefined,0,空字符串和false将为true。如果它太多,你需要特别检查你需要什么,比如null和undefined。

+0

嗨,感谢您的回复。对不起,来晚了 。你能否给出更容易理解的更简单的解决方案? –

+0

这是我能想到的最简单的。你不明白什么?关于这个下划线表示法,请参考 –

+0

“if(_.some(fields,f => _.get(updatevalues,f)=== null))”,请提供一些URL来阅读更多关于它的信息?它来自underscore.js npm包吗?我实际上没有机会在underscore.js上工作。 –