I have a Laravel API that under a request gets some info from the database and returns a JSON. If I run it with postman everything works but when I do it through vanilla js I get the results correctly too but the js try/catch block determines it failed and skips the try code block.
This is the Laravel API method that returns the JSON:
public function getYearsModel(ModelVehicles $model){
$generations = \DB::table('generations')->where('model_id', $model->id)->get();
$start = $generations->min('starts');
$end = $generations->max('end');
$years = [];
for($start; $start <= $end; $start++){
array_push($years, "{'id': $start, 'name': $start}");
}
return response()->json(
[$years]
);
}
This is the js script:
const getYears = async (event) => {
try {
const years = await getApiResults(`url/${event.target.value}`);
if(years.length > 0) {
deleteOptions('year_select');
addOptions('year_select');
enableSelect('year_select');
} else {
deleteOptions('year_select');
disableSelect('year_select');
}
} catch (error) {
alert('Error when retrieving years');
}
}
const getApiResults = async (url) => {
const response = await fetch(url);
return await response.json();
}
On the network tab, I see it returns the JSON with the correct values also tinker returns correct values when I run the same query.
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire