gpt4 book ai didi

node.js - 如何使用 "find where"SailsJS 蓝图路由?

转载 作者:搜寻专家 更新时间:2023-10-31 22:39:16 25 4
gpt4 key购买 nike

用例

我想使用 SailsJS“Find Where”蓝图路径创建一个具有多个条件的复杂查询。但是,我无法成功使用 equals 比较器和 and 条件。我找不到关于如何实现 Find Where 路线的足够文档,所以我研究了 source code并提出了以下方案。

问题

使用 SailsJS Find Where Blueprint Route,如何实现:

  • 平等比较
  • 条件

成功案例

以下场景会返回相应的响应:

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"}}]}

最佳答案

要使用“and”查询,您可以使用与符号一起使用查询字符串语法和链条件。对于更高级的查询,如 OR 或复杂的运算符,最好编写 Controller 操作。如果您决定坚持使用蓝图,您可以使用编码为 JSON 的最有效的 Waterline 查询。

对于简单查询,您可以使用查询字符串方法。以下将为姓名和学校构建一个“和”查询。

http://localhost:1337/api/user?name=fred&school=foo

要将更高级的运算符链接在一起,应执行以下操作:

http://localhost:1337/api/user?where={"name":{"startsWith":"fred"},"path":{"endsWith":"fred"}}

关于node.js - 如何使用 "find where"SailsJS 蓝图路由?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25296387/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com