lux icon indicating copy to clipboard operation
lux copied to clipboard

Model scope method is not a function

Open fiftoine opened this issue 7 years ago • 0 comments

  • Platform: Windows 10 64-bit
  • Database: Mysql
  • Lux Version: 1.2.2
  • Node Version: 8.9.3

Considering this situation:

lux new testApp --database mysql lux g resource user email:string password:string

Updated routes.js

this.resource('users', function () {
   this.post('/emails', 'emails');
});

Added scope to model

class User extends Model {
  static scopes = {
      findByEmail(email) {
          return this.first().where({
              email: email
          });
      }
  };
}

Added method to controller

class UsersController extends Controller {
  params = [
    'email',
    'password'
  ];

  query = [
      'data'
  ];

  emails({
      params: {
          data: {
              attributes: {
                  identification
              }
          }
      }
  }) {
    return User.findByEmail(identification);
  }
}

When I POST http://localhost:4000/users/emails with body {"data":{"attributes":{"identification":"[email protected]"}}}

I receive a 500 with response :

{
    "errors": [
        {
            "status": "500",
            "title": "Internal Server Error",
            "detail": "User.findByEmail is not a function"
        }
    ],
    "jsonapi": {
        "version": "1.0"
    }
}

What is wrong in the situation for the method findByEmail not to be found? This scope method should be callable from the controller no?

fiftoine avatar Jan 03 '18 14:01 fiftoine