我一直在遵循Next.js 14的Vercel this学习路径。在链接部分,创建了一个Postgres数据库(在Vercel上),并使用给定的seed.js和placeholder-data.js脚本进行播种。(见下文)
然而,当我使用node -r dotenv/config ./scripts/seed.js
运行种子脚本时,我收到一个错误:
> node -r dotenv/config ./scripts/seed.js
Created "users" table
Seeded 1 users
Created "customers" table
Seeded 10 customers
Created "invoices" table
Error seeding invoices: NeonDbError: db error: ERROR: prepared statement "s142409" does not exist
Caused by:
ERROR: prepared statement "s142409" does not exist
at execute (/Users/ecki/Documents/Dev/Web/yourgix-v2/node_modules/@neondatabase/serverless/index.js:1539:48)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
at async Promise.all (index 0)
at async seedInvoices (/Users/ecki/Documents/Dev/Web/yourgix-v2/scripts/seed.js:67:30)
at async /Users/ecki/Documents/Dev/Web/yourgix-v2/scripts/seed.js:166:3 {
code: '26000',
sourceError: undefined
}
node:internal/process/promises:288
triggerUncaughtException(err, true /* fromPromise */);
^
NeonDbError: db error: ERROR: prepared statement "s142409" does not exist
Caused by:
ERROR: prepared statement "s142409" does not exist
at execute (/Users/ecki/Documents/Dev/Web/yourgix-v2/node_modules/@neondatabase/serverless/index.js:1539:48)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
at async Promise.all (index 0)
at async seedInvoices (/Users/ecki/Documents/Dev/Web/yourgix-v2/scripts/seed.js:67:30)
at async /Users/ecki/Documents/Dev/Web/yourgix-v2/scripts/seed.js:166:3 {
code: '26000',
sourceError: undefined
}
字符串
如果之后检查数据库条目,我会看到invoices表中的一些条目已经创建(比如10个中有8个)。
.env保存这些变量的值:
POSTGRES_URL
POSTGRES_PRISMA_URL
POSTGRES_URL_NON_POOLING
POSTGRES_USER
POSTGRES_HOST
POSTGRES_PASSWORD
POSTGRES_DATABASE
型
seed.js
const { sql } = require("@vercel/postgres");
const {
invoices,
customers,
revenue,
users,
} = require("../app/lib/placeholder-data.js");
const bcrypt = require("bcrypt");
async function seedUsers() {
try {
await sql`CREATE EXTENSION IF NOT EXISTS "uuid-ossp"`;
// Create the "invoices" table if it doesn't exist
const createTable = await sql`
CREATE TABLE IF NOT EXISTS users (
id UUID DEFAULT uuid_generate_v4() PRIMARY KEY,
name VARCHAR(255) NOT NULL,
email TEXT NOT NULL UNIQUE,
password TEXT NOT NULL
);
`;
console.log(`Created "users" table`);
// Insert data into the "users" table
const insertedUsers = await Promise.all(
users.map(async (user) => {
const hashedPassword = await bcrypt.hash(user.password, 10);
return sql`
INSERT INTO users (id, name, email, password)
VALUES (${user.id}, ${user.name}, ${user.email}, ${hashedPassword})
ON CONFLICT (id) DO NOTHING;
`;
})
);
console.log(`Seeded ${insertedUsers.length} users`);
return {
createTable,
users: insertedUsers,
};
} catch (error) {
console.error("Error seeding users:", error);
throw error;
}
}
async function seedInvoices() {
try {
await sql`CREATE EXTENSION IF NOT EXISTS "uuid-ossp"`;
// Create the "invoices" table if it doesn't exist
const createTable = await sql`
CREATE TABLE IF NOT EXISTS invoices (
id UUID DEFAULT uuid_generate_v4() PRIMARY KEY,
customer_id UUID NOT NULL,
amount INT NOT NULL,
status VARCHAR(255) NOT NULL,
date DATE NOT NULL
);
`;
console.log(`Created "invoices" table`);
// Insert data into the "invoices" table
const insertedInvoices = await Promise.all(
invoices.map(
(invoice) => sql`
INSERT INTO invoices (customer_id, amount, status, date)
VALUES (${invoice.customer_id}, ${invoice.amount}, ${invoice.status}, ${invoice.date})
ON CONFLICT (id) DO NOTHING;
`
)
);
console.log(`Seeded ${insertedInvoices.length} invoices`);
return {
createTable,
invoices: insertedInvoices,
};
} catch (error) {
console.error("Error seeding invoices:", error);
throw error;
}
}
async function seedCustomers() {
try {
await sql`CREATE EXTENSION IF NOT EXISTS "uuid-ossp"`;
// Create the "customers" table if it doesn't exist
const createTable = await sql`
CREATE TABLE IF NOT EXISTS customers (
id UUID DEFAULT uuid_generate_v4() PRIMARY KEY,
name VARCHAR(255) NOT NULL,
email VARCHAR(255) NOT NULL,
image_url VARCHAR(255) NOT NULL
);
`;
console.log(`Created "customers" table`);
// Insert data into the "customers" table
const insertedCustomers = await Promise.all(
customers.map(
(customer) => sql`
INSERT INTO customers (id, name, email, image_url)
VALUES (${customer.id}, ${customer.name}, ${customer.email}, ${customer.image_url})
ON CONFLICT (id) DO NOTHING;
`
)
);
console.log(`Seeded ${insertedCustomers.length} customers`);
return {
createTable,
customers: insertedCustomers,
};
} catch (error) {
console.error("Error seeding customers:", error);
throw error;
}
}
async function seedRevenue() {
try {
// Create the "revenue" table if it doesn't exist
const createTable = await sql`
CREATE TABLE IF NOT EXISTS revenue (
month VARCHAR(4) NOT NULL UNIQUE,
revenue INT NOT NULL
);
`;
console.log(`Created "revenue" table`);
// Insert data into the "revenue" table
const insertedRevenue = await Promise.all(
revenue.map(
(rev) => sql`
INSERT INTO revenue (month, revenue)
VALUES (${rev.month}, ${rev.revenue})
ON CONFLICT (month) DO NOTHING;
`
)
);
console.log(`Seeded ${insertedRevenue.length} revenue`);
return {
createTable,
revenue: insertedRevenue,
};
} catch (error) {
console.error("Error seeding revenue:", error);
throw error;
}
}
(async () => {
await seedUsers();
await seedCustomers();
await seedInvoices();
await seedRevenue();
})();
型
placeholder-data.js
// This file contains placeholder data that you'll be replacing with real data in the Data Fetching chapter:
// https://nextjs.org/learn/dashboard-app/fetching-data
const users = [
{
id: '410544b2-4001-4271-9855-fec4b6a6442a',
name: 'User',
email: '[email protected]',
password: '123456',
},
];
const customers = [
{
id: '3958dc9e-712f-4377-85e9-fec4b6a6442a',
name: 'Delba de Oliveira',
email: '[email protected]',
image_url: '/customers/delba-de-oliveira.png',
},
{
id: '3958dc9e-742f-4377-85e9-fec4b6a6442a',
name: 'Lee Robinson',
email: '[email protected]',
image_url: '/customers/lee-robinson.png',
},
{
id: '3958dc9e-737f-4377-85e9-fec4b6a6442a',
name: 'Hector Simpson',
email: '[email protected]',
image_url: '/customers/hector-simpson.png',
},
{
id: '50ca3e18-62cd-11ee-8c99-0242ac120002',
name: 'Steven Tey',
email: '[email protected]',
image_url: '/customers/steven-tey.png',
},
{
id: '3958dc9e-787f-4377-85e9-fec4b6a6442a',
name: 'Steph Dietz',
email: '[email protected]',
image_url: '/customers/steph-dietz.png',
},
{
id: '76d65c26-f784-44a2-ac19-586678f7c2f2',
name: 'Michael Novotny',
email: '[email protected]',
image_url: '/customers/michael-novotny.png',
},
{
id: 'd6e15727-9fe1-4961-8c5b-ea44a9bd81aa',
name: 'Evil Rabbit',
email: '[email protected]',
image_url: '/customers/evil-rabbit.png',
},
{
id: '126eed9c-c90c-4ef6-a4a8-fcf7408d3c66',
name: 'Emil Kowalski',
email: '[email protected]',
image_url: '/customers/emil-kowalski.png',
},
{
id: 'CC27C14A-0ACF-4F4A-A6C9-D45682C144B9',
name: 'Amy Burns',
email: '[email protected]',
image_url: '/customers/amy-burns.png',
},
{
id: '13D07535-C59E-4157-A011-F8D2EF4E0CBB',
name: 'Balazs Orban',
email: '[email protected]',
image_url: '/customers/balazs-orban.png',
},
];
const invoices = [
{
customer_id: customers[0].id,
amount: 15795,
status: 'pending',
date: '2022-12-06',
},
{
customer_id: customers[1].id,
amount: 20348,
status: 'pending',
date: '2022-11-14',
},
{
customer_id: customers[4].id,
amount: 3040,
status: 'paid',
date: '2022-10-29',
},
{
customer_id: customers[3].id,
amount: 44800,
status: 'paid',
date: '2023-09-10',
},
{
customer_id: customers[5].id,
amount: 34577,
status: 'pending',
date: '2023-08-05',
},
{
customer_id: customers[7].id,
amount: 54246,
status: 'pending',
date: '2023-07-16',
},
{
customer_id: customers[6].id,
amount: 666,
status: 'pending',
date: '2023-06-27',
},
{
customer_id: customers[3].id,
amount: 32545,
status: 'paid',
date: '2023-06-09',
},
{
customer_id: customers[4].id,
amount: 1250,
status: 'paid',
date: '2023-06-17',
},
{
customer_id: customers[5].id,
amount: 8546,
status: 'paid',
date: '2023-06-07',
},
{
customer_id: customers[1].id,
amount: 500,
status: 'paid',
date: '2023-08-19',
},
{
customer_id: customers[5].id,
amount: 8945,
status: 'paid',
date: '2023-06-03',
},
{
customer_id: customers[2].id,
amount: 8945,
status: 'paid',
date: '2023-06-18',
},
{
customer_id: customers[0].id,
amount: 8945,
status: 'paid',
date: '2023-10-04',
},
{
customer_id: customers[2].id,
amount: 1000,
status: 'paid',
date: '2022-06-05',
},
];
const revenue = [
{ month: 'Jan', revenue: 2000 },
{ month: 'Feb', revenue: 1800 },
{ month: 'Mar', revenue: 2200 },
{ month: 'Apr', revenue: 2500 },
{ month: 'May', revenue: 2300 },
{ month: 'Jun', revenue: 3200 },
{ month: 'Jul', revenue: 3500 },
{ month: 'Aug', revenue: 3700 },
{ month: 'Sep', revenue: 2500 },
{ month: 'Oct', revenue: 2800 },
{ month: 'Nov', revenue: 3000 },
{ month: 'Dec', revenue: 4800 },
];
module.exports = {
users,
customers,
invoices,
revenue,
};
型
我已经在连接字符串上使用和不使用?pgbouncer=true
进行了尝试。我在Node v18.18.2和v20.9.0上运行了此操作。我还销毁了数据库,并使用DEALLOCATE ALL
创建了一个新数据库。
所有这些尝试都没有改变结果。
3条答案
按热度按时间mec1mxoz1#
下面是我在Github上找到的修复后的seed.js,它可以工作:
https://github.com/vercel/next-learn/blob/271f7db0da7ecba44e06b60b22d0fde802cf19c0/dashboard/final-example/scripts/seed.js
lmvvr0a82#
在我的例子中,从Vercel复制secrets片段时出现了一个问题。如果你的脚本有问题,最好在Vercel中仔细检查你的.env和postgres secrets。
qpgpyjmq3#
故障排除:
1.在将数据库复制到.env文件之前,请确保揭示数据库机密。
我验证了复制隐藏的机密值将POSTGRES_USER和POSTGRES_PASSWORD空“”粘贴到我的本地.env中
1.如果您在播种数据库时遇到任何问题并希望再次运行脚本,则可以通过在数据库查询界面中运行DROP TABLE tablename删除任何现有表。有关详细信息,请参阅下面的执行查询部分。但请注意,此命令将删除表及其所有数据。可以在示例应用中执行此操作,因为您正在使用占位符数据,但你不应该在生产应用程序中运行此命令。