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!
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.
What happens when you try to log in?
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
thanks trycatch solved this
what if i need to call this hook on findById only not always.
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
Discord
online
Get dedicated engineering support directly from the Payload team.