How to use the find where SailsJS route?

Usage example

I would like to create a complex query with more than one criterion using the SailsJS Find Where "route plan. However, I cannot use the equals compator as well as the condition successfully. I could not find adequate documentation on how to implement the Find route Where, so I worked through the source code and came up with the following scripts.

Question

Using SailsJS Find Where Blueprint Route, how to implement this:

  • equality comparison
  • and condition

Success scenarios

The following scripts will return the appropriate response:

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

Failure Scenario

The following scripts return an empty response:

 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"}}]} 
+5
source share
1 answer

To use "and" queries, you use the query string syntax and chain criteria together with the ampersand character. For more complex queries, such as OR or complex operators, it is best to write a controller action. If you decide to stick with the drawings, you can use the most current Waterline requests encoded as JSON.

For simple queries, the query string method is used. The following will build an โ€œandโ€ name and school query.

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

To combine more advanced operators, you must:

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

Source: https://habr.com/ru/post/1200274/


All Articles