如何在SAILJS的API REST中使用方法过滤器搜索?
问题描述:
您好我有问题时,我想生成sailjs 此代码生成API REST API休息:如何在SAILJS的API REST中使用方法过滤器搜索?
sails generate api dentist
,这将是生成路径URL像/create
,/update
,/destroy
,并/dentist
我的问题是在路径/dentish
我得到我的表中的所有行的列表,但我怎么可以通过使用过滤器(添加条件)获取行自定义?
注意: 我无法在sailjs网站上找到关于Rest API的文档,如果有任何文档对此有帮助的话。
答
您可以在您的请求中使用参数。所以,当您发送GET请求到/dentish
时,您会收到表中的所有行。但是,如果您通过?id=5
发送GET请求/dentish
,则只能获得与ID匹配的元素。为了得到这个,你必须明确地编程它,因为你实际上只有一个端点,/dentish
。
配置/ routes.js
'GET /dentish' : {controller : "Example", action: "get"},
API/ExampleController.js
get : function(req, res){
// Initialize filters as empty JSON
var filters = {};
// If the parameter key has been sent with the request...
if (req.param("key") != "undefined"){
// ... we store the key with its value in the filters JSON
filters["key"] = req.param("key");
}
// We have built our filters, we send the query...
Model.find(filters).exec(function(err,resuls){
// ...and show results
console.log(results)
});
}
做我的回答可以帮助您?请通知我,我会按照尝试帮助你 –