postgresql Render/prisma.卡住5小时,无效的`prisma.user.findUnique()`调用:无法到达数据库服务器,

66bbxpm5  于 12个月前  发布在  PostgreSQL
关注(0)|答案(1)|浏览(159)

我的用户注册过程中遇到了一个问题,特别是与数据库连接有关的问题。当我在Postman上提交前端表单或测试post请求时,我得到了一个500内部服务器错误。我的PostgreSQL数据库和后端都托管在Render上,我已经确认我的环境变量是正确的。

prisma.schema:

prisma
model User {
  user_id      Int       @id @default(autoincrement())
  signup_date  DateTime  @default(now())
  password     String
  email        String    @unique
  phone        String
  first_name   String
  last_name    String
  address      String
  addressLine2 String?
  city         String
  state        String
  zip          String
  products     Product[]
  orders       Order[]
}

字符串

/auth/auth.js:

router.post('/register', async (req, res, next) => {
    const { password, email, phone, first_name, last_name, address, addressLine2, city, state, zip } = req.body;

    try {
        // Check if a user with the given email already exists
        const existingUser = await prisma.user.findUnique({
            where: { email: email },
        });

        if (existingUser) {
            return res.status(400).json({ message: 'Email already in use' });
        }

        // If no existing user is found, proceed with creating a new user
        const hashedPassword = await bcrypt.hash(password, saltRounds);
        const newUser = await prisma.user.create({
            data: {
                password: hashedPassword,
                email,
                phone,
                first_name,
                last_name,
                address,
                addressLine2,
                city,
                state,
                zip
            },
        });

        // Create a JWT token for the new user
        const token = jwt.sign({ user_id: newUser.user_id }, process.env.JWT_SECRET, { expiresIn: '1h' });

        // Send the token in the response
        res.status(201).json({ token });
    } catch (error) {
        console.error(error.message);
        next(error);
    }
});

main.js:

const authRoutes = require('./auth/auth');
app.use('/auth', authRoutes);

渲染时间:

Invalid `prisma.user.findUnique()` invocation: Can't reach database server at *********:5432 Please make sure your database server is running at *********:5432. PrismaClientInitializationError:  Invalid `prisma.user.findUnique()` invocation: Can't reach database server at dpg-cm110gen7f5s73e325tg-a:5432 Please make sure your database server is running at *********:5432.
    at si.handleRequestError (/opt/render/project/src/node_modules/@prisma/client/runtime/library.js:125:7117)
    at si.handleAndLogRequestError (/opt/render/project/src/node_modules/@prisma/client/runtime/library.js:125:6151)
    at si.request (/opt/render/project/src/node_modules/@prisma/client/runtime/library.js:125:5859)
    at async l (/opt/render/project/src/node_modules/@prisma/client/runtime/library.js:130:10025)
    at async /opt/render/project/src/src/server/auth/auth.js:16:30

**Postman raw JSON body:**

    {   "email": "[email protected]",   "password": "yourPassword",   "first_name": "Test",   "last_name": "Doe",   "phone": "123432123",   "address": "123 Test Street",   "addressLine2": "Apt 1",   "city": "Test City",   "state": "Test State",   "zip": "12345" }


我试了内部和外部URL,在本地使用表单并部署,甚至注解掉了CORS中间件,似乎后端和数据库之间存在连接问题,我无法弄清楚这是怎么回事。

relj7zay

relj7zay1#

你不会在任何地方的任何帖子中找到这个问题的答案,但是当这个错误发生时,这是一个Prisma迁移问题。你的Prisma schema/database不同步。只需运行

npx prisma migrate dev

字符串

相关问题