0

I am trying to get some user data from stripe in the getServerSideProps function in Next.JS so that I can pass it down to some other components but I am having a hard time obtaining the data from the getServerSideProps function.

Below is my getServerSideProps function:

export const getServerSideProps = withPageAuthRequired({

  async getServerSideProps(context) {
    const user = getSession(context.req, context.res).user

    const resources = await table.select({}).all()
    const customer = await fetch(`/api/customers`).then(res => res.json()).then(data => {
      data.data.find(user_data => user_data.metadata['auth0_user_id'] === user.sub);})

    const subscriber_status = await customer.metadata['subscription_status'] === 'true';


    return {
      props: {
        tech_resources: minifyRecords(resources),
        subscriber_stats: subscriber_status, // I want to return the subscriber status so that I can pass it to another component later on
      }
    }
  }

});

Below is my original fetch request which obtains the data I am looking for If I use it as a standalone function or with a useEffect hook.

fetch(`/api/customers`).then(res => res.json()).then(data => {
            const customer = data.data.find(user_data => user_data.metadata['auth0_user_id'] === user.sub);
            if (customer.metadata['subscription_status'] === 'true') {
                // Do Something;}}

However, when I tried this inside of getServerSideProps it does not seem to work. Can anyone help me out on this?

  • Note that requests from inside `getServerSideProps` are made from the server and as such do not automatically send cookies like client-side requests do. You may have to explicitly pass the required cookies (to authenticate the user) to the API call. See [Why are cookies not sent to the server via getServerSideProps in Next.js?](https://stackoverflow.com/questions/69057271/why-are-cookies-not-sent-to-the-server-via-getserversideprops-in-next-js). – juliomalves May 29 '22 at 18:06

1 Answers1

0

The await fetch then using the promise looks strange to me. When you use await, then your result will be the response.

So maybe try

const response = await fetch(`/api/customers`);
const data = await response.json();
Matthew Barnden
  • 516
  • 1
  • 6
  • 15
  • OP can also check if the real request has been made from https://dashboard.stripe.com/test/logs (Stripe Test API logs) – orakaro May 30 '22 at 03:02