postlight / lux

Build scalable, Node.js-powered REST JSON APIs with almost no code.
https://lux.postlight.com
MIT License
570 stars 60 forks source link

Model scope method is not a function #727

Open fiftoine opened 6 years ago

fiftoine commented 6 years ago

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":"xxx@yyy.com"}}}

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?