嗨,当我想在 sailjs 中生成 api 休息时,我遇到了问题 这里是生成 api 休息的代码:
sails generate api dentist
这将生成路径 url,例如 /create
、 /update
、 /destroy
和 /dentist
我的问题是在路径/dentish
内,我获取表中所有行的列表,但是如何通过使用过滤器获取行(添加条件)来自定义它?
注意: 我在 sailjs 站点中找不到有关 Rest API 的文档,如果有相关文档,将会很有帮助。
最佳答案
您可以在请求中使用参数。因此,当您向 /dentish
发送 GET 请求时,您会收到表中的所有行。但是,如果您使用 ?id=5
发送 GET 请求 /dentish
,您只能获取与 ID 匹配的元素。要实现这一点,您必须对其进行显式编程,因为您实际上只有一个端点,/dentish
。
config/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)
});
}
关于javascript - 如何在 SAILS JS 的 API REST 中使用方法过滤器搜索?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33820541/