Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

StrongLoop Explorer Persisted Model Error

Tags:

strongloop

I am trying to set up a sample strong loop application and I'm getting the following error when I try to tryout the post api using /explorer.

"Cannot call AccessToken.findById(). The findById method has not been setup.. The PersistedModel has not been correctly attached to a DataSource."

I ran through and installed slc loopback:acl with the following settings:

"acls": [
    {
      "accessType": "*",
      "principalType": "ROLE",
      "principalId": "admin",
      "permission": "ALLOW"
    }
  ],

However, these settings do not seem to apply to my model.

like image 942
Brandon Wolvin Avatar asked Nov 01 '22 08:11

Brandon Wolvin


2 Answers

I had the same problem: what solved this for me was to delete my browser's Local Storage (in "Resources" tab in developper tools). The explanation to this was I'd been using another loopback app which uses token-based auth.

like image 99
Marc Perrin-Pelletier Avatar answered Jan 04 '23 13:01

Marc Perrin-Pelletier


I have the same error message and the solution is not available as everyone has there own thought.

So What works for me If you extended AccessToken according to reference given in loopback-component-passport,

Just don't remove base AccessToken from model-config keep both. Don't worry as loopback only going to use extended model to keep Token.

model-config.json

`'....
,
  "AccessToken": {
    "dataSource": "adnexux",
    "public": false
  },
  "accessToken": {
    "dataSource": "adnexux",
    "public": false
  },
...'`

Then Just add this to middleware.json

`....
,
    "loopback#token": {
      "params": {
        "model": "accessToken"
      }
    }
  },
...`

If Error Is without Extending the accessToken:

Then Just check model config if model is connected to data source.

`....
  "AccessToken": {
    "dataSource": "adnexux",
    "public": false
  },
.....`

And To get current User you can paste this code in server.js

`'app.use(function setCurrentUser(req, res, next) {
  if (!req.accessToken) {
    return next();
  }
  app.models.user.findById(req.accessToken.userId, function(err, user) {
    if (err) {
      return next(err);
    }
    if (!user) {
      return next(new Error('No user with this access token was found.'));
    }
    res.locals.currentUser = user;
    next();
  });
});'`
like image 35
user3057481 Avatar answered Jan 04 '23 14:01

user3057481