QueryError

default discord avatar
ronok992
6 months ago
15

How to handle QueryError? In payload 1.8.2

  • default discord avatar
    notchr
    6 months ago

    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 @paulpopus for second opinion

  • default discord avatar
    paulpopus
    6 months ago

    @ronok992 how exactly are you getting an error?



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



    await fetch().then().catch( error => do something)
  • default discord avatar
    ronok992
    6 months ago

    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

  • default discord avatar
    paulpopus
    6 months ago

    can you post your code so its reproducible



    ?

Open the post
Continue the discussion in Discord
Like what we're doing?
Star us on GitHub!

Star

Connect with the Payload Community on Discord

Discord

online

Can't find what you're looking for?

Get help straight from the Payload team with an Enterprise License.