【问题标题】:How can I create a CRUD method in a model using Loopback?如何使用 Loopback 在模型中创建 CRUD 方法?
【发布时间】:2019-05-09 20:28:13
【问题描述】:

我必须在 Loopback 项目中添加一个删除方法,而我从未接触过有关 Loopback 的东西。我知道 express 和 NodeJS 但回环? → 零

但我知道您可以使用向导创建模型,并且此模型附带所有方法(PUT、POST、GET、UPDATE、DELETE,...),但我正在尝试编辑的项目是模型“注册表”只有 1 个方法 POST,我需要方法 Delete 我认为,如何获得?

我正在搜索文档和其他页面,但一无所获:/ 有什么解决办法吗? 提前致谢!

我尝试使用远程方法,但出现此错误:

Unhandled error for request GET /Attendant/api/v1/registries/greet?msg=test: Error: Shared class "registry" has no method handling GET /greet?msg=test

这是我在 registry.js 中的代码

....
Registry.greet = function(msg, cb) {
    cb(null, 'Greetings... ' + msg);
}

Registry.remoteMethod('greet', {
    accepts: { arg: 'msg', type: 'string' },
    returns: { arg: 'greeting', type: 'string' }
});
....

在 mu registry.json 中:

...
    "methods": {
        "getHour": {
            "accepts": [],
            "returns": {
                "arg": "data",
                "root": true
            },
            "http": {
                "verb": "get",
                "path": "/hours/current"
            }
        },
        "createRegistry": {
            "accepts": [{
                "arg": "req",
                "type": "object",
                "required": true,
                "http": {
                    "source": "req"
                }
            }],
            "returns": {
                "arg": "data",
                "root": true
            },
            "http": {
                "verb": "post",
                "path": "/"
            }
        },
        "greet": {
            "http": {
                "verb": "get",
                "path": "/greet"
            }
        }
    }
...

【问题讨论】:

    标签: node.js express loopback


    【解决方案1】:

    这是添加“问候”远程方法所需的示例。

    Registry.greet = function(msg, cb) {
       cb(null, 'Greetings... ' + msg);
    }
    
    Registry.remoteMethod('greet', {
       http: { path: '/greet', method: 'get' },
       accepts: { arg: 'msg', type: 'string', http: { source: 'query' } },
       returns: { arg: 'greeting', type: 'string' }
    });
    

    您无需更改 JSON 模型参考中的任何内容。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-14
      • 2017-10-20
      • 2017-03-21
      • 2019-06-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多