Mike
Mike

Reputation: 371

Customizing models

I am trying to customize models according to this link, https://docs.strongloop.com/display/public/LB/Customizing+models

common/models/MyModel.js

module.exports = function(Review) {
    Review.on('dataSourceAttached', function(obj) {
        var find = Review.find

        Review.find = function(filter, cb) {
            // cb is not a function

        }
    }

}

Response Body

{
    "error": {
        "name": "TypeError",
        "status": 500,
        "message": "cb is not a function",
        "stack": "TypeError: cb is not a function\n at Function.Review.find (/home/test/dev/common/models/review.js:19:7)\n at Function.findOne (/home/test/dev/node_modules/loopback-datasource-juggler/lib/dao.js:1546:8)\n at SharedMethod.invoke (/home/test/dev/node_modules/loopback/node_modules/strong-remoting/lib/shared-method.js:248:25)\n at HttpContext.invoke (/home/test/dev/node_modules/loopback/node_modules/strong-remoting/lib/http-context.js:384:12)\n at /home/test/dev/node_modules/loopback/node_modules/strong-remoting/lib/remote-objects.js:620:11\n at execStack (/home/test/dev/node_modules/loopback/node_modules/strong-remoting/lib/remote-objects.js:460:7)\n at RemoteObjects.execHooks (/home/test/dev/node_modules/loopback/node_modules/strong-remoting/lib/remote-objects.js:464:10)\n at /home/test/dev/node_modules/loopback/node_modules/strong-remoting/lib/remote-objects.js:617:10\n at /home/test/dev/node_modules/loopback/lib/application.js:357:13\n at /home/test/dev/node_modules/loopback/lib/model.js:313:7"
    }
}

Review.find is called from these 3 API and only find() work and the rest does not provide callback.

According to the API doc, it should, https://apidocs.strongloop.com/loopback/#persistedmodel-findone

findById() GET /reviews/:id
find() GET /reviews
findOne() GET /reviews/findOne

Any help????

Many Thanks

Upvotes: 1

Views: 594

Answers (2)

Matt Schlobohm
Matt Schlobohm

Reputation: 1378

I also have had this error, "cb is not a function".

In my case, it was because my local method definition and remote method declaration did match, the local method had a parameter that wasn't declared for the remote method.

Something like this:

module.exports = (MyModel) => {
  MyModel.sayHi = (thisIsNotDeclaredInRemoteMethod, cb) => {
    cb(null, "Hello there!");
  };

  MyModel.remoteMethod("sayHi", {
    http: {verb: "get"},
    returns: {arg: "msg", type: "string"}
  });
};

Hope this helps, good luck! :)

Upvotes: 1

ebarault
ebarault

Reputation: 126

as stated in https://docs.strongloop.com/display/public/LB/Customizing+models, you need to add

return find.apply(this, arguments);

Upvotes: 1

Related Questions