laravel-strapi icon indicating copy to clipboard operation
laravel-strapi copied to clipboard

An unknown Strapi error was returned while calling Route http://127.0.0.1:8000/test/2

Open bast111 opened this issue 3 years ago • 5 comments

Discussed in https://github.com/dbfx/laravel-strapi/discussions/15

Originally posted by bast111 April 6, 2022 Hi guys, being fairly new to Laravel i wanted to start implement a strapi backend following that tutorial here

All worked fine at first, I created a route to fetch all my localites

Route::get('/test', function () {
    $strapi = new LaravelStrapi();
    return $localites = $strapi->collection('api/localites');
});

image

So I kept going and tried to create the route for one item

//Route pour 1 localite
Route::get('/test/{id}', function ($id) {
    $strapi = new LaravelStrapi();
    return $localites = $strapi->entry('api/localites', $id);
});

And this is what I get : image

I can see that the api has been called successfully but I don't understand what is the problem image

If anyonce can give me some insights would be really appreciated :) Thanks

bast111 avatar May 31 '22 10:05 bast111

I have the exact same problem, which is caused by line 98 of the LaravelStrapi.php vendor class:

        if (!isset($entry['id'])) {
            Cache::forget($cacheKey);

            if (null === $entry) {
                throw new NotFound('The requested single entry (' . $type . ') was null');
            }

            throw new UnknownError('An unknown Strapi error was returned');
        }

As we can see in the screenshot postet above, the data returned via the API has a key "data" at the root level of the response. It seems the check for the entry in the LaravelStrapi.php class is not aware of that and only checks for the key "id", but lacks check of the "data" key. I changed this to:

if (!isset($entry['data']['id'])) {

and that resolves the problem, but is still a change in the vendor-file...

eblumstengel-dbkg avatar Jun 11 '22 23:06 eblumstengel-dbkg

Thanks all, please try v2.0.2 now and see if fixed?

dbfx avatar Jun 13 '22 09:06 dbfx

Tested & works! Thank you very much!

eblumstengel-dbkg avatar Jun 13 '22 11:06 eblumstengel-dbkg

Amazing guys Thx for the fix it now works fine!

I do have another problem tho reaching the end of the tutorial. I did a Route calling the Localite function in the LocaliteController. which return the localites.blade.php view... so far everything works fine But when trying to access the fields from my Strapi object like the 'nom' or 'npa' it can't be found even id. image

Could anyone enlight me please :D

bast111 avatar Jun 14 '22 11:06 bast111

Here's a var_dump of the localite variable image

bast111 avatar Jun 14 '22 11:06 bast111