英文:
Create User function is not working in node.js using express
问题
以下是您要翻译的内容:
以下是我的函数所在的位置,createUser 在 const {firstName, id} = req.body 生效之前不起作用,虽然名称和 ID 显示出来,但不会创建用户。如果您需要更多信息,请随时提问。谢谢!
const { PrismaClient } = require("@prisma/client");
const prisma = new PrismaClient();
module.exports = {
async createUser(req, res) {
try {
const {firstName, id} = req.body
console.log({firstName, id}) //直到这里,名称和 ID 生效,但不会创建用户
const user = await prisma.UserInfo.create({
data: {
firstName,
id
}
});
return res.json(user)
} catch (error) {
return res.json({error})
}
},
async teste(req, res) {
try {
return res.json("again")
}
catch(error) {
return res.json({error})
}
}
}
这是您的路由部分:
const express = require("express");
const UserController = require("./UserController");
let router = express.Router();
router.post("/user", UserController.createUser);
module.exports = router
这是您的服务器部分:
const express = require('express');
const router = require('./routes.js');
const app = express();
app.use(express.json())
app.use(require("./routes.js"))
app.listen(3333, () => {
console.log('server at port 3333 is running');
});
这是您在 Prisma 中的模式:
generator client {
provider = "prisma-client-js"
}
datasource db {
provider = "sqlserver"
url = env("DATABASE_URL")
shadowDatabaseUrl = env("DATABASE_SHADOW_URL")
}
model UserInfo {
id Int @id @default(autoincrement())
firstName String?
lastName String?
email String @unique
password String
age Int
role String?
}
英文:
below is where my function is, and the createUser is not working, until const {firstName, id} = req.body is working the name and id show up but don't create the user. If you need more informations just ask. Thank you!
const { PrismaClient } = require("@prisma/client");
const prisma = new PrismaClient();
module.exports = {
async createUser(req, res) {
try {
const {firstName, id} = req.body
console.log({firstName, id}) //until here is working the name and id show up but don't create the user
const user = await prisma.UserInfo.create({
data: {
firstName,
id
}
});
return res.json(user)
} catch (error) {
return res.json({error})
}
},
async teste(req, res) {
try {
return res.json("again")
}
catch(error) {
return res.json({error})
}
}
}
that image show what error show up in my screen
here is my routes
const express = require("express");
const UserController = require("./UserController");
let router = express.Router();
router.post("/user", UserController.createUser);
module.exports = router
here is my server
const express = require('express');
const router = require('./routes.js');
const app = express();
app.use(express.json())
app.use(require("./routes.js"))
app.listen(3333, () => {
console.log('server at port 3333 is running');
});
here is my schema in prisma
generator client {
provider = "prisma-client-js"
}
datasource db {
provider = "sqlserver"
url = env("DATABASE_URL")
shadowDatabaseUrl = env("DATABASE_SHADOW_URL")
}
model UserInfo {
id Int @id @default(autoincrement())
firstName String?
lastName String?
email String @unique
password String
age Int
role String?
}
答案1
得分: 0
You have passed the object to prisma.UserInfo.create() method.
根据我看到的错误消息,根据我的理解,您需要将属性email、password和age传递给Prisma.UserInfo.create(),同时包括属性id和firstName。
问题在于,当您要在userInfo表中创建记录时,您只传递了属性id和firstName。但是在您的数据库迁移中,您设置了email、password和age这些属性不能为null。
因此,必须将以下属性传递给prisma.UserInfo.create()方法:
const { email, password, age, firstName } = req.body;
prisma.UserInfo.create({
data: { email, password, age, firstName } // firstName是可选的
});
(Note: I've provided the translation as requested, excluding the code portion.)
英文:
You have passed the object to prisma.UserInfo.create() method.
As per my understanding after seeing the error message, You need to pass the properties email, password, and age to Prisma.UserInfo.create() with the property id and firstName.
Here the problem is, When you going to create the record in the userInfo table, you have only passed the property id and firstName. But in your database migration, you set that like email, password, and age these things are not going to be null.
So the following properties must be passed to prisma.UserInfo.create() method
const {email, password, age, firstName} = req.body;
prisma.UserInfo.create({
data: {email, password, age, firstName // firstName is optional
}
});
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论