O
O
Oleg Vdovenko2018-05-05 01:15:24
JavaScript
Oleg Vdovenko, 2018-05-05 01:15:24

How to overcome the car of if'ov?

I am writing a chatbot on nodejs, there are several data sets that it collects via webhook, but they all belong to the same topic - transportation. Accordingly, the database has a model - transportation, which includes all fields from different types of transportation (freight, cars, etc.).
It turns out that when data for passenger transportation arrives, it does not contain the data required for cargo transportation, and vice versa. I think on the server I just check whether there is such and such a parameter, and if not, then I don’t write it to an object, which is then saved to the database. Car if'ov, but otherwise did not figure out how to do it, the parameter names may not match those that come. True, in this case I get the error "Unhandled error for request POST /webhook/: TypeError: Cannot read property 'startTime' of undefined" from the condition of one of the if's.
Why doesn't it convert it to FALSE? How can I fix it and do you have any ideas how to organize it better? I understand that it's clumsy, but I have no idea what to do with it.

const transportationData = {
    employeeId: req.body.originalDetectIntentRequest
      .payload.data.userProfile.id,
    'created-date': todayDate.toISOString(),
    'request-status': 'обрабатывается',
    'transportation-type': req.body
      .queryResult.parameters['transportation-type'],
  };

  if (req.body.queryResult.parameters['places-count']) {
    transportationData['places-count'] = req.body.queryResult
      .parameters['places-count'];
  }

  if (req.body.queryResult.parameters['contact-person']) {
    transportationData['contact-person'] = req.body.queryResult
      .parameters['contact-person'];
  }

  if (req.body.queryResult.parameters['stowing-type']) {
    transportationData['stowing-type'] = req.body.queryResult
      .parameters['stowing-type'];
  }

  if (req.body.queryResult.parameters['cargo-type']) {
    transportationData['cargo-type'] = req.body.queryResult
      .parameters['cargo-type'];
  }

  if (req.body.queryResult.parameters['unloading-type']) {
    transportationData['unloading-type'] = req.body.queryResult
      .parameters['unloading-type'];
  }

  if (req.body.queryResult.parameters['loading-type']) {
    transportationData['loading-type'] = req.body.queryResult
      .parameters['loading-type'];
  }

  if (req.body.queryResult.parameters['trailer-type']) {
    transportationData['trailer-type'] = req.body.queryResult
      .parameters['trailer-type'];
  }

  if (req.body.queryResult.parameters['truck-type']) {
    transportationData['truck-type'] = req.body.queryResult
      .parameters['truck-type'];
  }

  if (req.body.queryResult.parameters['grain-type']) {
    transportationData['grain-type'] = req.body.queryResult
      .parameters['grain-type'];
  }

  if (req.body.queryResult.parameters['location-from']) {
    transportationData['location-from'] = req.body.queryResult
      .parameters['location-from'];
  }

  if (req.body.queryResult.parameters['location-to']) {
    transportationData['location-to'] = req.body.queryResult
      .parameters['location-to'];
  }

  if (req.body.queryResult.parameters['unloading-time']) {
    transportationData['unloading-time'] = req.body.queryResult
      .parameters['unloading-time'];
  }

  if (req.body.queryResult.parameters['loading-time']) {
    transportationData['loading-time'] = req.body.queryResult
      .parameters['loading-time'];
  }

  if (req.body.queryResult.parameters.period.startTime) {
    transportationData['period-start'] = req.body.queryResult.parameters
      .period.startTime;
    transportationData['period-end'] = req.body.queryResult.parameters
      .period.endTime;
  }

  if (req.body.queryResult.parameters.compatibility) {
    transportationData.compatibility = req.body.queryResult.parameters
      .compatibility;
  }

  if (req.body.queryResult.parameters.urgency) {
    transportationData.urgency = req.body.queryResult.parameters.urgency;
  }

  if (req.body.queryResult.parameters.date) {
    transportationData.date = req.body.queryResult.parameters.date;
  }

  if (req.body.queryResult.parameters['loading-time'].startTime) {
    transportationData['loading-time-start'] = req.body.queryResult
      .parameters['loading-time'].startTime;
    transportationData['loading-time-end'] = req.body.queryResult
      .parameters['loading-time'].endTime;
  } else if (req.body.queryResult.parameters['loading-time']) {
    transportationData['loading-time'] = req.body.queryResult
      .parameters['loading-time'];
  }

  if (req.body.queryResult.parameters['unloading-time'].startTime) {
    transportationData['unloading-time-start'] = req.body.queryResult
      .parameters['unloading-time'].startTime;
    transportationData['unloading-time-end'] = req.body.queryResult
      .parameters['unloading-time'].endTime;
  } else if (req.body.queryResult.parameters['unloading-time']) {
    transportationData['unloading-time'] = req.body.queryResult
      .parameters['unloading-time'];
  }

Answer the question

In order to leave comments, you need to log in

3 answer(s)
P
profesor08, 2018-05-05
@olhapi

Go around them in a cycle, everything is the same for you.

for (let p in req.body.queryResult.parameters) {
  if (req.body.queryResult.parameters.hasOwnProperty(p)) {
    transportationData[p] = req.body.queryResult.parameters[p];
  }
}

P
Pavlo Ponomarenko, 2018-05-05
@TheShock

If you don't like the idea of ​​a loop for some reason (for example, you want to explicitly specify a list of parameters), you can create an anonymous function

function saveParam (title) {
  var param = req.body.queryResult.parameters[title];
  if (param) {
    transportationData[title] = param;
  }
}

saveParam('places-count');
saveParam('contact-person');
saveParam('stowing-type');
saveParam('cargo-type');

T
TCloud, 2018-05-05
@OTCloud

Push the name of the parameters into a variable and use switch ... case ...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question