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.

Not able to login after using beforeRead in the admin panel . Using this hook in users collection

default discord avatar
abdullahasad7253last year
5
 beforeRead: [
      async ({
        doc, // full document data
        req, // full express request
        query, // JSON formatted query
      }) => {
        if (doc.offer) {
          console.log("doc has offer");
          console.log(doc.offer);
          const offerId = doc.offer;
          const offers = await payload.find({
            collection: "offer",
            where: {
              and: [
                { id: { in: { offerId } } },
                {
                  offer_expiry: { less_than: Date.now() },
                },
              ],
            },
          });
          console.log("offer", offers);
        }
        return doc;
      },
    ],

i want to check if the offer is expired for a user , But i am not able to login to admin panel!

  • default discord avatar
    akhilnaidu_last year

    comment out the api call, and check if you can login. If yes, then the issue would be that your request got failed and never returned the doc, leaving to login issues.



    use try catch to debug such issues and return the doc, irrespective of the error.

  • default discord avatar
    markatomniuxlast year

    What happens when you try to log in?

  • default discord avatar
    akhilnaidu_last year

    when you are trying to login, it logged in and sent the details to the beforeRead doc, but as the api called failed and throwed the error in beforeRead, it never returned the doc you are returning in your hooj

  • default discord avatar
    abdullahasad7253last year

    thanks trycatch solved this



    what if i need to call this hook on findById only not always.

  • default discord avatar
    markatomniuxlast year

    you can check if the request is a GET request, and if it contains a param for the id. Probably called "id"



    The req object contains everything you need 🙂

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.