2016-08-24 1 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 ...

はところで、あなたはおそらく、フィールドが存在していることを確認するので、彼らがnullかどうかをチェックすることは正しくありません。 。フィールドがない場合、undefinednullではありません。 !_.get(updatevalues, f)を確認することができます。 null、未定義、0、空文字列、および偽の場合はtrueです。それが多すぎる場合は、nullやundefinedのように具体的に必要なものを確認する必要があります。

+0

こんにちは、お返事ありがとうございます。遅刻してすみません 。簡単に理解できる簡単な解決策を教えてください。 –

+0

これは私が考えることができる最も簡単です。あなたは何を理解していますか? –

+0

このアンダースコア表記に関しては、 "if(_.some(fields、f => _.get(updatevalues、f)=== null))"、もっと詳しく読むためのURLを教えてください。 underscore.js npmパッケージからですか?私は実際にアンダースコアで作業するチャンスを得ることができませんでした。 –

関連する問題