Skip to content

A better way how to include belongsTo model in response #165

Closed

Description

@bajtos

A follow-up for #161, where we modified User.login to fill accessToken.__data.user with the current user.

Once #162 is fixed, it may be tempting to change that line to accessToken.user. However, there is a caveat.

Because AccessToken belongsTo User, it already has a user property of type function (see loopback-datasource-juggle/lib/relations.js#L244).

If User.login overrides this property value then the code calling User.login directly may stop working.

User.login(credentials, function(err, token) {
  token.user(function(err, user) {
    // do something
  });
});

I feel there should be a better way for exposing the belongsTo models in the JSON response, possibly similar to the solution in loopbackio/loopback-datasource-juggler#64. @raymondfeng could you advise?

Activity

  1. raymondfeng commented on Feb 12, 2014

    @raymondfeng
    Member

    You should be able to build the response as follows with loopback-datasource-juggler 1.3.x.

    user.setStrict(false);
    user.accessToken = ...;

  2. bajtos commented on Feb 12, 2014

    @bajtos
    MemberAuthor

    Did you mean this?

    token.setStrict(false);
    token.user = user;
    

    As I mentioned in the issue description, token.user is already defined by belongsTo User relationship, it's a function to get/set the related user model.

    This is what I am looking for:

    token.user(user);
    // set a flag telling token.toJSON() to include 
    // the cached user in the response
    
  3. bajtos commented on Feb 23, 2014

    @bajtos
    MemberAuthor

    Related: #198

  4. raymondfeng commented on Feb 26, 2014

    @raymondfeng
    Member

    You should be able to do token.user(user) today. Can you try the following?

    token.user(user);
    token.setStrict(false); // If the statement above is not enough

  5. self-assigned this
    on Feb 28, 2014
  6. bajtos commented on Oct 22, 2014

    @bajtos
    MemberAuthor

    I am closing this issue, since there was not enough people asking for this enhancement and it clearly was not a priority for us so far. We can reopen it again later when there is more demand.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions