2

So...I have an api in Next js that uses Prisma Client. Prisma is imported from the global object defined in prisma.ts

Locally everything builds and runs fine. I get no errors and the prisma variable is defined. However, when it's deployed in Vercel, prisma is undefined...I can't work out why.

If anyone has any suggestions, I'd much appreciate it.

import eBayApi from "@hendt/ebay-api";
import prisma from "../../lib/prisma";

const eBay = new eBayApi({});

export default async (req, res) => {
  // Access the provided 'page' and 'limt' query parameters
  const code = req.query.code; // this is provided from eBay
  console.log(code);

  try {
    //const token = await eBay.OAuth2.getToken(code);
    const token = "bob";

    console.log("Prisma handler instance", prisma);

    const env_variable = await prisma.variable.upsert({
      where: {
        variable: "EBAY_TOKEN",
      },
      update: { value: token },
      create: {
        variable: "EBAY_TOKEN",
        value: token,
      },
    });

    if (env_variable) {
      console.log("New Token Stored in DB");
    } else console.log("Failed to store new Token");

    res.status(200);
    res.writeHead(302, {
      Location: "/orders",
      //add other headers here...
    });
    res.end();
  } catch (e) {
    console.error(e);
    res.status(400).end();
  }

  res.writeHead(302, {
    Location: "/orders",
    //add other headers here...
  });
  res.end();
};

2021-04-18T19:06:18.680Z 869eb228-423a-4d6a-b05a-f95f5e843c88 ERROR TypeError: Cannot read property 'upsert' of undefined at exports.modules.5712.webpack_exports.default (/var/task/nextjs-store/.next/server/pages/api/success.js:55:126) at processTicksAndRejections (internal/process/task_queues.js:93:5) at async apiResolver (/var/task/nextjs-store/node_modules/next/dist/next-server/server/api-utils.js:8:1) at async Server.handleApiRequest (/var/task/nextjs-store/node_modules/next/dist/next-server/server/next-server.js:67:462) at async Object.fn (/var/task/nextjs-store/node_modules/next/dist/next-server/server/next-server.js:59:492) at async Router.execute (/var/task/nextjs-store/node_modules/next/dist/next-server/server/router.js:25:67) at async Server.run (/var/task/nextjs-store/node_modules/next/dist/next-server/server/next-server.js:69:1042) at async Server.handleRequest (/var/task/nextjs-store/node_modules/next/dist/next-server/server/next-server.js:34:504) at async Server. (/var/task/nextjs-store/___next_launcher.js:26:9)

2
  • The error is saying "upsert" is undefined, not Prisma. "prisma.variable.upsert" specifically. Is that the right name? Commented Apr 21, 2021 at 2:09
  • It's 'upsert of undefined' i.e. cannot read the valid upsert of the undefined object prisma.variable The upsert is correct as far as i can tell, and being used elsewhere prisma.io/docs/reference/api-reference/… Commented Apr 21, 2021 at 6:30

1 Answer 1

1

So, I had a play around, and think I found the problem. My prisma table field was a VARCHAR (String), however I was inadvertently trying to store upsert with a JSON object. Now that I've changed to a JSON field it's working.

So I guess the only problem is that maybe the error wasn't helpul? Although it was all my stupid fault.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.