gpt4 book ai didi

mysql - Loopbackjs - findById 需要 id 参数

转载 作者:行者123 更新时间:2023-11-29 10:58:45 25 4
gpt4 key购买 nike

我是环回新手,我正在尝试在环回中创建一个使用 findyById 方法的简单远程方法。已经花了几个小时在这上面,但仍然无法让它工作。这是我的代码:

客户.js:

    Customer.list = function(customerId, cb){
app.models.Customer.findById(customerId, function (err, instance) {
cb(null, err || 'success');
if(err){
console.log(err);
}else if(instance){
console.log(instance);
}
});
}

// expose the above method through the REST
Customer.remoteMethod('list', {
returns: {
arg: 'list',
type: 'string'
},
accepts: {arg: 'id', type: 'number', http: { source: 'query' } },
http: {
path: '/list',
verb: 'get'
}
});

customer.controller.js:

    Customer.list(1)
.$promise
.then(function(response){
console.log(response);
})
.catch(function(err){
console.log(err);
});

我在 mysql 中的客户行:

id:1 号码:10

我收到此错误:

    Error: Model::findById requires the id argument
at Desktop\SampleProject\node_modules\loopback-datasource-juggler\lib\dao.js:1287:10
at _combinedTickCallback (internal/process/next_tick.js:67:7)
at process._tickCallback (internal/process/next_tick.js:98:9)

您能告诉我出现此错误的可能原因吗?请帮我。谢谢

最佳答案

对于 get 动词,没有主体。

您需要像这样更改远程方法:

 accepts: {arg: 'id', type: 'number', http: { source: 'path' } },
http: {
path: '/list/:id',
verb: 'get'
}

关于mysql - Loopbackjs - findById 需要 id 参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42604039/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com