定制模型

时间:2015-10-20 00:41:36

标签: loopbackjs

我正在尝试根据此链接自定义模型, https://docs.strongloop.com/display/public/LB/Customizing+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

        }
    }

}

回应机构

{
    "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"
    }
}

从这3个API调用Review.find,只有find()工作,其余的不提供回调。

根据API文档,它应该是https://apidocs.strongloop.com/loopback/#persistedmodel-findone

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

任何帮助????

非常感谢

2 个答案:

答案 0 :(得分:1)

https://docs.strongloop.com/display/public/LB/Customizing+models中所述,您需要添加

return find.apply(this, arguments);

答案 1 :(得分:1)

我也遇到过这个错误," cb不是一个功能"。

在我的情况下,这是因为我的本地方法定义和远程方法声明确实匹配,本地方法有一个参数,没有为远程方法声明。

这样的事情:



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

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




希望这有帮助,祝你好运! :)