A
A
alexnotonfire2016-04-12 00:50:42
Angular
alexnotonfire, 2016-04-12 00:50:42

How to get correct response from API? Error: [filter:notarray] Expected array but received?

I need to send a request to the API and get a response in the following form

workers: Array[2]
count_items: 2

But the thing is, I get this error in the console:
Error: [filter:notarray] Expected array but received: 
{"workers":
[{"id":15,"user_id":17,"company_id":20,"created_at":"2016-04-08T10:15:59.775Z","updated_at":"2016-04-08T10:15:59.775Z","info":{"first_name":"George","last_name":"Lucas","email":"[email protected]"},"deleted_at":null,"name":"dsadsa dsadsa"},
{"id":16,"user_id":5,"company_id":20,"created_at":"2016-04-08T10:16:08.446Z","updated_at":"2016-04-08T10:16:08.446Z","info":{"first_name":"Sam","last_name":"Mitch","email":"[email protected]"},"deleted_at":null,"name":"dsad dsadas"}],
"count_items":2}

Here is my Rails API action:
class CompanyWorkersController < ApplicationController
  def index
    render json: { workers:   company.company_workers.page(params[:page])
                  .as_json(methods: :name),
                   count_items: company.company_workers.length }
  end
end

My factory :
factory('CompanyWorkers', [
  '$resource', function($resource) {
    return $resource('/company_workers/:id/:action', {id: '@id'}, 
      { 'query': { isArray: false } }
    );
  }
]);

And finally, an API request:
CompanyWorkers.query({ page: options.page }, function(response) {

  console.log(response);

});

What am I doing wrong here and how can I fix the problem? Thanks in advance.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Evgeny Kumanin, 2016-04-12
@alexnotonfire

It's not about $recourse, it works correctly.
The filter throws an error. https://github.com/angular/angular.js/issues/9992#...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question