#QueryError

15 messages · Page 1 of 1 (latest)

chrome void
#

How to handle QueryError? In payload 1.8.2

undone hornet
#

There are a couple of patterns to go about error handling with queries, but overall it's similar to any other asynchronous function handling

#

For example, with the REST API

#
import qs from 'qs';

const query = {
  color: {
    equals: 'mint',
  },
  // This query could be much more complex
  // and QS would handle it beautifully
}

const getPosts = async () => {
  const stringifiedQuery = qs.stringify({
    where: query // ensure that `qs` adds the `where` property, too!
  }, { addQueryPrefix: true });

  const response = await fetch(`http://localhost:3000/api/posts${stringifiedQuery}`);
  // Continue to handle the response below...
}
#

This example from the official documentation shows the getPosts call, which is an async function

#

You can wrap that whole function in a try/catch block

#

That way, you can handle the errors in specific ways (query error / server error / etc)

#

Using the same example above, it would look like this

#
try {
  const getPosts = async () => {
  const stringifiedQuery = qs.stringify({
    where: query // ensure that `qs` adds the `where` property, too!
  }, { addQueryPrefix: true });

  const response = await fetch(`http://localhost:3000/api/posts${stringifiedQuery}`);
  // Continue to handle the response below...
  }
} catch (error) {
  // check "error" and respond to it accordingly, maybe show the client a message
}
#

Note that this try/catch pattern will catch errors for both the qs.stringify method and the fetch request

#

You typically always want to try/catch asynchronous methods. And in case I'm wrong, pinging @twilit scarab for second opinion

twilit scarab
#

@chrome void how exactly are you getting an error?

#

^ this looks right, you can also catch your errors via

await fetch().then().catch( error => do something)
chrome void
#

Oh no.. not for api call..I error when returning query in access control, Previously I had a common auth which user to return an or query where the field name was from different collection. But after update the QueryBuilder was update to build query will field that are available in specific collection. Like {or:[{userId:id}],{user:id}]} if a collection does not have both userId and user field it will give error. But Previously the query use to return result if the doc match any of the or condition

twilit scarab
#

can you post your code so its reproducible