query local data in afterChange hook

default discord avatar
akacronos
8 months ago
2

I'm trying to send an email each time a user creates a visit on my website, I have a collection called visits with two relationships [users, activity], in the afterChange hook I just get the user and activity id's, so I was trying to query the full data and then send the email, but always get

Promise { <pending> }

hooks: {
    afterChange: [
      ({ doc }) => {
        const { attendee, visitType, activity } = doc;

        const getAtendee = async attendeeId => {
          const atendeeData = await payload.find({
            collection: 'users',
            where: {
              id: {
                equals: attendeeId,
              },
            },
          });

          return atendeeData;
        };

        console.log(getAtendee(attendee));
        // payload.sendEmail(ActivityRegister()); // This is the email
      },
    ],
  },


I don't know if this is the right way to get the full data :S

  • default discord avatar
    Jarrod
    8 months ago

    First off, you can get the user off the

    req

    which is an argument just like

    doc

    , using req.user. That way you don’t have to query the users collection. But the reason you are logging a pending promise is because you would need to

    await

    getAttendee since it is async.



    However, I would just change the hook to async (by adding

    async

    before the fn), and get rid of the inner async function wrapper.

  • default discord avatar
    akacronos
    8 months ago

    I need the data from the user in the relationship, not always it’s going to be the same that creates the doc. I’m going to change the async fn to see if that fix the query. Thanks!!!

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.