我是loopback的新手,我正在尝试使用findyById方法在loopback中创建一个简单的远程方法。我花了几个小时在这上面,但还是不能让它工作。下面是我的代码:
customer.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中的My customer行:
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)您能告诉我出现此错误的可能原因吗?请帮帮我。谢谢
发布于 2017-03-05 11:30:02
有了get动词,就没有身体。
您需要更改远程方法,如下所示:
accepts: {arg: 'id', type: 'number', http: { source: 'path' } },
http: {
path: '/list/:id',
verb: 'get'
}https://stackoverflow.com/questions/42604039
复制相似问题