下一个 js prisma 在 vercel 部署时未定义
next js prisma undefined when in vercel deployment
所以...我在使用 Prisma Client 的 Next js 中有一个 api。 Prisma 是从 prisma.ts
中定义的全局对象导入的
在本地,一切都能正常构建和运行。我没有收到任何错误,并且定义了 prisma 变量。
但是,当它部署在 Vercel 中时,prisma 是未定义的...我不明白为什么。
如果有人有任何建议,我将不胜感激。
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)
所以,我试了一下,认为我找到了问题所在。我的 prisma table 字段是一个 VARCHAR(字符串),但是我无意中试图用 JSON 对象存储 upsert。
现在我已经更改为 JSON 字段,它正在工作。
所以我想唯一的问题是错误可能没有帮助?
虽然都是我的蠢错。
所以...我在使用 Prisma Client 的 Next js 中有一个 api。 Prisma 是从 prisma.ts
中定义的全局对象导入的在本地,一切都能正常构建和运行。我没有收到任何错误,并且定义了 prisma 变量。 但是,当它部署在 Vercel 中时,prisma 是未定义的...我不明白为什么。
如果有人有任何建议,我将不胜感激。
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)
所以,我试了一下,认为我找到了问题所在。我的 prisma table 字段是一个 VARCHAR(字符串),但是我无意中试图用 JSON 对象存储 upsert。 现在我已经更改为 JSON 字段,它正在工作。
所以我想唯一的问题是错误可能没有帮助? 虽然都是我的蠢错。