Awesome q2a theme

How to overcome car IFS ever existed.

0 like 0 dislike
Write the chatbot in nodejs, there are multiple sets of data it collects via webhook but they all relate to one theme - transport. Respectively in the DB is a model of transportation, which includes all the fields from the different types of transport (trucks, cars etc).

It turns out that when you come data for passenger transportation, there is no data required for freight, and Vice-versa. I think the server is just checking whether a certain parameter and if present it does not write to the object, which is then stored in the database. Car IFS ever existed, but otherwise not figured out how to do the parameter names may not coincide with those that come. However in this case you get error "Unhandled error for request POST /webhook/: TypeError: Cannot read property 'startTime' of undefined" from the condition of one of the IFS ever existed.

Why is it not converts it to FALSE? How to fix and maybe there are ideas how it is better to organize? Understand that ugly, but what to do with it I'll never know.

const transportationData = { employeeId: req.body.originalDetectIntentRequest 'created-date': todayDate.toISOString(), 'request-status': 'processed', '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 ( { =; } 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']; }
by | 32 views

3 Answers

0 like 0 dislike
Go around their cycle, you're all the same.

for (let p in req.body.queryResult.parameters) { if (req.body.queryResult.parameters.hasOwnProperty(p)) { transportationData[p] = req.body.queryResult.parameters[p]; } }
0 like 0 dislike
If you don't like the idea of a cycle for some reason (for example, I want to explicitly specify the 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');
0 like 0 dislike
Shove the name of the parameters in a variable and I switch ... case...
110,608 questions
257,187 answers
40,796 users