0

After entering my application my GlobalApplicationController initializes some services used through the whole application (like subscribing to sockets, etc.). It would be pretty handy to have a CurrentUser object - but I'm not sure about the object I get

@app.factory 'CurrentUser', ($http) ->
  user = $http({
    method: 'GET',
    url: 'users/me'
  }).then (data) ->
    return data

The returned object contains a $$state object. To get to the actual data I need to call CurrentUser.$$state.value.data.

I want my service to be called like CurrentUser.email (or whatever attribute), but I have no idea what type this returned object is or why I'm getting it.

Edit: I've found out that the returned object I get is the $http object (due to coffeescript). Is there a way to return the data (not a promise)?

1 Answer 1

1

From $http General Usage then() docs:

The response object has these properties:

data – {string|Object} – The response body transformed with the transform functions.
status – {number} – HTTP status code of the response.
headers – {function([headerName])} – Header getter function.
config – {Object} – The configuration object that was used to generate the request.
statusText – {string} – HTTP status text of the response.

So you would want to change

return data

To

return data.data
Sign up to request clarification or add additional context in comments.

1 Comment

I've found out that the returned object is actually the $http object (due to coffeescripts), not the returned value within the then(). Assigning the value of the returned data to a variable and return it isn't possible, since the promise isn't fulfilled at the time the function is called (at least I think so). I'm not too familiar with promises, but any ideas how to solve that?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.