$ http angularjs return not expected

2

Description

I'm implementing a $ http request with angular. I want to return the objects from the database.

Code

CompanyService.coffee

angular.module('vaultfactor').factory 'CompanyService', ($http, $q) ->
getCompanies: ->
    promise = $http.get "/companies"
    company = promise.then((response)->
        if typeof response.data == 'object'
            console.log(response.data)
            return response.data
        else
            return $q.reject(response.data)
    (response)->
        $q.reject(response.data))

main_ctrl.coffee

angular.module('vaultfactor').controller 'HomeCtrl', ($scope, $rootScope, 
    $location, $http, CompanyService) ->
        console.log(CompanyService.getCompanies())

Problem

The returned object is not the template you expected.

Givenaconsole.loginmyresponse.databeforethereturnitreturnsmeexactlywhatIneed.so:(It'sbeingpassedinthedependencies$q,I'vepostedanolderimage.)

Console.log:

What am I missing in the return of my success?

    
asked by anonymous 27.11.2014 / 13:38

1 answer

2

Try this way on the service:

angular.module('vaultfactor').factory 'CompanyService', ($http, $q) ->
getCompanies: ->
    $http.get "/companies"

And where to use the list:

angular.module('vaultfactor').controller 'HomeCtrl', ($scope, $rootScope, 
    $location, $http, CompanyService) ->
        promise = CompanyService.getCompanies()
        company = promise.then((response)->
            if typeof response.data == 'object'
                console.log(response.data)
                return response.data
            else
                return $q.reject(response.data)

Remember that this works if the return is an object, if it is array you need to change the get per query as in the example below:

$http.query "/companies"
    
27.11.2014 / 14:56