jadjoubran / laravel5-angular-material-starter

Get started with Laravel 5.3 and AngularJS (material)
https://laravel-angular.readme.io/
MIT License
1.66k stars 400 forks source link

Expected array but received {} #326

Closed ghost closed 8 years ago

ghost commented 8 years ago

Hi

I'm trying to show data in my table, but browser throws this error

Error: [orderBy:notarray] Expected array but received: {}

And table shows no data.

My table directive has orderBy filter

...
$scope.refresh = function(resetPage) {
                if(resetPage === true) {
                    $scope.page = 0;
                }
                $scope.contents = $filter('orderBy')($scope.contents, activeSortColumn, activeSortOrder);
            };
...

I get data with getList function and Laravel sends this response

{"ex_puolisots":[{"id":1,"created_at":"2016-05-31 18:08:43","updated_at":"2016-05-31 18:08:43","deleted_at"
:null,"firstname":"sdf\u00f6lk\u00f6jsadf","lastname":"ewqproiuewrup","paattymispaiva":"2016-05-01","ositus"
:1},{"id":2,"created_at":"2016-06-01 18:17:40","updated_at":"2016-06-01 18:17:40","deleted_at":null,"firstname"
:"Joku","lastname":"Niminen","paattymispaiva":"2016-04-10","ositus":0}]}

Tried to fix with this and this guide, but no success.

flick36 commented 8 years ago

Yeah, you will have to be more especific, amm how are you getting the data from laravel? and what variable has that data?, i can only asume its $scope.contents but i dont know how you get that data and also you are not using ES6 syntax so, are you using this starter or you just asking for help?

jadjoubran commented 8 years ago

I think the issue here is that you can't use getList() if your api is returning a JSON object. You would need to explicitly return the array contents.

Before

class UsersController{
    public function get(){
         $users = App\User::get();

         return $users;
   }
}

After

class UsersController{
    public function get(){
         $users = App\User::get();

         return $users->toArray();
   }
}
ghost commented 8 years ago

Yep, errors are gone and "return $users->toArray();" works, thanks a lot!

However my table is empty, but I think thats because response array contains to much data fields =)