Error in production, hope anyone could give me a hint

38 Views Asked by At

i'm deploying an app on vercel, it's an ecommerce app with next-auth for authentication. here it's the error:

TypeError: Cannot read properties of null (reading 'id')
    at a (/vercel/path0/.next/server/app/dashboard/page.js:1:2927)
    at c (/vercel/path0/.next/server/app/dashboard/page.js:1:3107)
    at processTicksAndRejections (node:internal/process/task_queues:95:5)
    at runNextTicks (node:internal/process/task_queues:64:3)
    at listOnTimeout (node:internal/timers:538:9)
    at process.processTimers (node:internal/timers:512:7)
TypeError: Cannot read properties of null (reading 'id')
    at a (/vercel/path0/.next/server/app/dashboard/page.js:1:2927)
    at c (/vercel/path0/.next/server/app/dashboard/page.js:1:3107)
Error occurred prerendering page "/dashboard". Read more: https://nextjs.org/docs/messages/prerender-error

here is /app/dashboard/page.tsx

import { getOrders } from '../actions/getOrders'
import getCurrentUser from '../(auth)/actions/getCurrentUser'
import Image from 'next/image'
import formatPrice from '@/utils/formatPrice'

const Dashboard = async () => {
  const user = await getCurrentUser()
  const orders = await getOrders(user)
  return (
    <div>
      {user ? (
        <div className="main-container">
          <div className="p-8 flex items-center justify-center gap-12 text-center">
            <div>
              <p className="text-2xl">Ciao, {user?.name}</p>
              <p>{user?.email}</p>
            </div>
          </div>
          <div>
            <h1 className="font-bold text-xl text-center underline">Ordini</h1>

            {orders && orders.length > 0 ? (
              <>
                {orders?.map((order) => (
                  <div
                    key={order.id}
                    className="rounded-lg p-8 my-4 space-y-2 bg-gray-200"
                  >
                    <h2 className="text-xs font-medium">
                      Ordine numero: {order.id.replaceAll(/\D/g, '')}
                    </h2>
                    <p className="text-xs">Stato: {order.status}</p>

                    <div className="text-sm lg:flex items-center gap-4">
                      {order.items.map((product) => (
                        <div key={product.id} className="py-2">
                          {product.image && (
                            <Image
                              src={product.image}
                              width={100}
                              height={100}
                              alt={`image for ${product.name}`}
                            />
                          )}

                          <h2 className="py-1">{product.name}</h2>
                          <span className="text-xs">{product.size}</span>
                          <div className="flex items-baseline gap-4">
                            <p>Quantity: {product.quantity}</p>
                          </div>
                        </div>
                      ))}
                    </div>
                    <p className="font-medium py-2">
                      Totale: {formatPrice(order.amount)}
                    </p>
                  </div>
                ))}
              </>
            ) : (
              <div>
                <h1>Nessun ordine piazzato</h1>
              </div>
            )}
          </div>
        </div>
      ) : (
        <div className="h-96 flex items-center justify-center text-2xl uppercase">
          Esegui il log in per visualizzare i tuoi ordini
        </div>
      )}
    </div>
  )
}
export default Dashboard

I hope someone could tell me how to fix the error, i've looked on the web on how to do so but still i couldn't succed, hope in one of your hint.

I hope in your hint to fix the error and deploy the app soon.

1

There are 1 best solutions below

1
Vipin Bhatt On

It seems like the error is occurring because the id property of the order object is null or undefined. The error message "Cannot read properties of null (reading 'id')" suggests that the code is trying to access the id property of a null or undefined value.

To be specific I think this block is causing the error :

<h2 className="text-xs font-medium">
Ordine numero: {**order.id**.replaceAll(/\D/g, '')}
</h2>

You will have to handle this properly, for example :

<h2 className="text-xs font-medium">
Ordine numero: {order.id ? order.id.replaceAll(/\D/g, '') : 'N/A'}
</h2>