I was hoping for some help on how to handle these object in case of errors. I want to ensure each object has data that I can process before I send it to the functions. What I am finding that in the case something is up with object1 execution goes to catch statement even thought I could have doTwo function run normally.
try {
const response = await fetch('my_script.php');
const data = await response.json();
let object_1 = [JSON.parse(data['first'][0]), JSON.parse(data['first'][1])];
let object_2 = [JSON.parse(data['second'][0]), JSON.parse(data['second'][1])];
let object_3 = [JSON.parse(data['third'][0]), JSON.parse(data['third'][1])];
let object_4 = [JSON.parse(data['forth'][0]), JSON.parse(data['forth'][1])];
doOne(object_1, object_2);
doTwo(object_3, object_4);
}
catch (error) {
console.log("catch");
console.error('This is the error -> :', error)
}
yes and $response1[1] is http code which i can use for error checking. everything works as it should and I am mostly curious about how to handle the part of the code that I shared above. I was hoping to still be able to do something with other objects in case one has error
Wait… if response1[1] is an HTTP code, not a JSON string, and you try to JSON.parse it, it will throw an error. This is why i specifically asked you if all of those are JSON strings…are they?
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
// Check for errors
if ($httpCode >= 400) {
// Handle error, for example, log it or throw an exception
return array (false, $httpCode);
}
// Close cURL session
curl_close($ch);
//return data from the API call as a string
return array ($response, $httpCode);
ok have to go get little refresher…
java script calls php script which makes 4 api calls
api calls returns the string and http code as an integer
I put those two into an array and each of these 4 arrays are put into associative array in PHP and send it out via echo statement echo json_encode()