mongodb 在Postman中发送POST请求时,prisma.user.create()调用无效

9wbgstp7  于 2023-08-04  发布在  Go
关注(0)|答案(1)|浏览(109)

该项目使用Expressjs,react,mongodb(在localhost上)和prisma。当我发送post请求时,它会抛出此错误:
{“msg”:“\n无效的prisma.user.create()调用:\n\n{\n数据:{\n firstName:'sepehr',\n ~~\n姓氏:'shirazi',\n ~\n电子邮件:'jfsijf@gmail.com',\n密码:'1234'\n }\n}\n\nUserCreateInput类型的data.firstName中有未知参数firstName。可用参数:\n\ntype UserCreateInput {\n id?:String\n name?:字符串|空\n电子邮件:字符串\n密码:字符串\n帖子?:PostCreateNestedManyWithoutAuthorInput\n}\nUserCreateInput类型的data.lastName中有未知参数lastName。你是说name吗可用参数:\n键入UserCreateInput {\n id?:String\n name?:字符串|空\n电子邮件:字符串\n密码:字符串\n帖子?:PostCreateNestedManyWithoutAuthorInput\n}\n\n”}
这段代码有什么问题?prisma.schema:

generator client {
  provider = "prisma-client-js"
}

datasource db {
  provider = "mongodb"
  url      = env("DATABASE_URL")
}

model User {
  id    String @id @default(auto()) @map("_id") @db.ObjectId
  firstName String
  lastName  String
  email String? @unique
  password String
      posts Post[]

}

字符串
我的路线:

import express from 'express'
import { getUser, getProductById, newUser, updateProduct, deleteProduct } from '../controller/ProductController.js'

const router = express.Router()

router.get('/users', getUser)
router.get('/products/:id', getProductById)
router.post('/users', newUser)
router.patch('/products/:id', updateProduct)
router.delete('/products/:id', deleteProduct)

export default router


我的一个控制器:

export const newUser = async (req, res) => {
    const { firstName, lastName, email, password } = req.body
    try {
        const user = await prisma.user.create({
            data: {
                firstName: firstName,
                lastName: lastName,
                email: email,
                password: password
            },
        })
        res.status(201).json(user)
    } catch (error) {
        res.status(400).json({ msg: error.message })
    }
}

5lhxktic

5lhxktic1#

每次修改Prisma模式后,都应该运行prisma generateprisma db push

相关问题