在Loopback.js中为模型添加远程功能

问题描述 投票:1回答:1

我在loopback.js模型中添加了一个远程函数,工作正常,当我添加另一个具有不同名称和url的函数时,新函数可以工作但前一个函数开始给出错误500。

我试过更改我正在调用的函数名称,更改api url和所有这些东西,但它没有用

第一个功能是这个

Station.remoteMethod(
    '_updateStation', {
        http: { path: '/update', verb: 'post' },
        accepts: [
            { arg: 'service', type: 'object', required: true, http: { source: 'body' } },
            {
                arg: 'ip', type: 'string', required: true, http: function (ctx) {
                    var req = ctx.req;
                    return req.headers['x-forwarded-for'] ||  req.connection.remoteAddress;
                }
            }
        ],
        returns: [
            { arg: 'status', type: 'string', description: ' stations update status' },
            { arg: 'statusCode', type: 'string', description: ' stations update code' }  
        ]
    }
);

新功能是

Station.remoteMethod(
    '_updateMultiple', {
        http: { path: '/updateall', verb: 'post' },
        accepts: [
            { arg: 'service', type: 'object', required: true, http: { source: 'body' } },
            {
                arg: 'ip', type: 'string', required: true, http: function (ctx) {
                    var req = ctx.req;
                    return req.headers['x-forwarded-for'] ||  req.connection.remoteAddress;
                }
            }
        ],
        returns: [
            { arg: 'status', type: 'string', description: ' stations all update status' },
            { arg: 'updateData', type: 'string', description: ' stations all update code' },
            { arg: 'stations', type: 'string', description: ' stations all update code' }    
        ]
    }
);

如果我注释掉新函数,/ update api工作正常,但如果取消注释这个新函数,则/ update api给出错误500

node.js loopbackjs
1个回答
0
投票

我解决了这个问题,很简单。只需禁用要覆盖的模型功能然后添加功能,它就可以工作。例如,如果要在模型上添加自定义/更新API,则需要执行此操作。

    _Model.disableRemoteMethodByName('update', false);

然后使用/ update url添加新的远程方法,它将起作用。因为如果你不禁用模型远程函数,并且创建/更新,它仍然会调用模型内置/更新方法,而不是你的自定义/更新方法。

© www.soinside.com 2019 - 2024. All rights reserved.