Я выполняю оператор IF, который проверяет, является ли ответ JSON пустым, чтобы выполнить следующую функцию, однако она выполняет обе функции.Ниже приведен пример ответа JSON с 3 объектами в «count_business», пример моей функции приведен ниже.Я бегу. Длина <1, но, кажется, не проверять это.</p>
JSON
{
"count_business": 3,
"business": [
{
"id": "Business.3b725ab1-1eeb-4f11-bb41-0a97cdd5475a",
"name": "Dogwood Valley Press LLC",
"industry": [
"Sporting Goods, Hobby, Musical Instrument, and Book Stores"
],
"found_at_address": {
"id": "Location.86fc8e6b-6dfb-446d-9488-aae59fea6eb2",
"location_type": "Address",
"street_line_1": "1604 Lincoln Ln",
"street_line_2": null,
"city": "Rolla",
"postal_code": "65401",
"zip4": "2614",
"state_code": "MO",
"country_code": "US",
"lat_long": {
"latitude": 37.956933,
"longitude": -91.743859,
"accuracy": "RoofTop"
},
"is_active": true,
"delivery_point": "SingleUnit",
"link_to_business_start_date": "2016-06-12",
"link_to_business_end_date": null
},
Мой оператор if
if(res.data.count_business < 1){
fetchGooglePlace(data)
}else{
console.log(chalk.white('RUNNING WHITEPAGES'));
for(let i =0; i < res.data.business.length; i++ ){
console.log(res.data.business[i].name);
console.log(res.data.business[i].found_at_address.street_line_1);
/*Second loop to fetch phone numbers from array in whitePages JSON */
for (let x = 0; x < res.data.business[i].phones.length; x++) {
console.log(res.data.business[i].phones[x].phone_number);
console.log(' ');
}
}
}
})
После запуска приложения оно запускает API whitePages, а также функцию fetchGooglePlace.