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條件,以便代碼看起來更好。 和藹可親。
嗨,感謝您的回覆。對不起,來晚了 。你能否給出更容易理解的更簡單的解決方案? –
這是我能想到的最簡單的。你不明白什麼?關於這個下劃線表示法,請參考 –
「if(_.some(fields,f => _.get(updatevalues,f)=== null))」,請提供一些URL來閱讀更多關於它的信息?它來自underscore.js npm包嗎?我實際上沒有機會在underscore.js上工作。 –