如何使用“find哪里”SailsJS蓝图路线?
用例
我想创build一个复杂的查询,使用SailsJS“ Find Where ”蓝图路线,使用多个标准 。 但是,我无法使用等于比较器和条件成功。 我找不到有关如何实现Find Where路由的足够的文档,所以我通过源代码工作并提出了以下scheme。
题
使用SailsJS Find蓝图路线,如何实现:
- 平等比较
- 和条件
成功案例
以下情况将返回相应的响应:
http://localhost:1337/api/user?name=fred http://localhost:1337/api/user?where={"name":{"startsWith":"fred"}} http://localhost:1337/api/user?where={"name":{"endsWith":"fred"}} http://localhost:1337/api/user?where={"name":{"contains":"fred"}} http://localhost:1337/api/user?where={"name":{"like":"fred"}} http://localhost:1337/api/user?where={"or":[{"name":{"startsWith":"fred"}}]} http://localhost:1337/api/user?where={"or":[{"name":{"startsWith":"fred"}},{"path":{"endsWith":"fred"}}]}
失败场景
以下情况会返回一个空的响应:
http://localhost:1337/api/user?where={"name":{"equals":"fred"}} http://localhost:1337/api/user?where={"name":{"=":"fred"}} http://localhost:1337/api/user?where={"name":{"equal":"fred"}} http://localhost:1337/api/user?where={"and":[{"name":{"startsWith":"fred"}}]} http://localhost:1337/api/user?where={"and":[{"name":{"startsWith":"fred"}},{"path":{"endsWith":"fred"}}]}
要使用“和”查询,请使用“与”字符一起使用查询string语法和链条标准。 对于更高级的查询,如OR
或复杂的操作符,最好编写一个控制器操作。 如果你决定坚持蓝图,你可以使用最有效的水线查询编码为JSON。
对于简单的查询,您使用查询string方法。 以下将build立一个名称和学校的“和”查询。
http://localhost:1337/api/user?name=fred&school=foo
要链接更多高级操作员,以下内容应该可以工作:
http://localhost:1337/api/user?where={"name":{"startsWith":"fred"},"path":{"endsWith":"fred"}}