如何在SAILJS的API REST中使用方法过滤器搜索

How can I using method filter search in API REST of SAILJS?

本文关键字:使用方法 过滤器 搜索 REST API SAILJS      更新时间:2023-09-26

嗨,当我想在sailjs中生成api rest时,我遇到了问题此处生成api rest的代码:

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)
    });
}