Simplify your stack and build anything. Or everything.
Build tomorrow’s web with a modern solution you truly own.
Code-based nature means you can build on top of it to power anything.
It’s time to take back your content infrastructure.

QueryError

default discord avatar
ronok9922 years ago
4

How to handle QueryError? In payload 1.8.2

  • default discord avatar
    notchr2 years 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

    @858693520012476436

    for second opinion

  • default discord avatar
    paulpopus2 years ago
    @707015513825345537

    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
    ronok9922 years 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
    paulpopus2 years ago

    can you post your code so its reproducible



    ?

Star on GitHub

Star

Chat on Discord

Discord

online

Can't find what you're looking for?

Get dedicated engineering support directly from the Payload team.