提问人:بغية السالِك 提问时间:6/21/2023 更新时间:6/22/2023 访问量:67
我不明白为什么我不能在数据库中的现有表中放置值;
I fail to understand why I can't PUT values inside an existing table in a database ;/
问:
我在数据库中有一个预先创建的表,我想从前端(即由用户)插入值,但由于某种原因,我编写的代码无法实现我所寻求的:/
我仍然是一个初学者,所以对我苛刻,这样我就可以通过记住我在这里遇到的情况来学习。
下面是代码的服务器端:
const express = require("express");
const app = express();
const cors = require("cors");
const pool = require("./db");
const bodyParser = require("body-parser");
app.use(bodyParser.json());
app.use(cors());
app.use(express.json());
// input values
app.put("/admin/staff", async (req, res) => {
try {
const {adminstration} = await req.body;
// contains the admin table details to be added
const newadminstration = await pool.query("INSERT INTO adminstration (adminstration) VALUES ($1) RETURNING *", [adminstration]);
res.json(newadminstration.rows[0]);
console.log(newadminstration)
} catch (err) {
console.error(err.message);
}
});
app.listen(5000, () => {
console.log("server is running on port 5000")
});
下面是代码的前端端:
import React from 'react';
import { useState } from 'react';
import Box from '@mui/material/Box';
import TextField from '@mui/material/TextField';
import { Input} from '@mui/material';
export default function Adminfun() {
const [adminstration, setadminstration] = useState({admin_name:"", admin_email:"", admin_password:""});
const bbb= async (e) => {
e.preventdefault();
try {
const body = { adminstration };
const response = await fetch("http://localhost:5000/admin/staff", {
method: "PUT",
headers: {"Content-Type": "application/json"},
body: JSON.stringify(body)
});
console.log(response)
} catch (err) {
console.error(err.message)
}
}; return (
<Box
component="form"
sx={{
'& > :not(style)': { m: 1, width: '25ch' },
}}
noValidate
autoComplete="off">
<TextField id="outlined-basic" label="Name" variant="outlined" value={adminstration.admin_name}
onChange={(Event) =>
{const newAdminstration = {...adminstration};
newAdminstration.admin_name = Event.target.value;
setadminstration(newAdminstration)}}/>
{/* {setadminstration({...adminstration, admin_name: Event.target.value})}}/> */}
<TextField id="filled-basic" label="Email" variant="filled" value={adminstration.admin_email}
onChange={(Event) =>
{setadminstration({...adminstration, admin_email: Event.target.value})}}/>
<TextField id="standard-basic" label="Password" variant="standard" value={adminstration.admin_password}
onChange={(Event) =>
{const newAdminstration = {...adminstration};
newAdminstration.admin_password = Event.target.value;
setadminstration(newAdminstration)}}/>
<br></br>
<Input type='submit' onSubmit={bbb} >Submit</Input>
</Box>)
};
后端的 Node JS,PostgreSQL 数据库,前端的 React。
答:
0赞
بغية السالِك
6/21/2023
#1
更新:按照我的前辈 - @AdrianKlaver - 命令,我已经设法成功解决了这个问题,尽管发现了另一个问题 - 为此我发布了一个单独的问题 - 这是更新的代码:
app.put("/admin/staff", async (req, res) => {
const client = await pool.connect()
try {
await client.query('BEGIN')
console.log(req.body)
const {adminstration } = await req.body
const {admin_name, admin_email, admin_password} = adminstration
await client.query("INSERT INTO adminstration (admin_name, admin_email, admin_password) VALUES ($1, $2, $3) RETURNING *", [admin_name, admin_email, admin_password]);
await client.query('COMMIT')
return res.status(201).send("ok")
}
catch (err) {
await client.query('ROLLBACK')
throw err
// console.error(err.message);
} finally {
client.release()
}
});
经过进一步的尝试,我发现这个问题从一开始就与事务无关,而是需要我进一步解构json对象,因为数据库出于某种原因不接受它。
在这里,它正在处理原始代码并进行一些编辑:
app.put("/admin/staff", async (req, res) => {
// const client = await pool.connect()
try {
console.log(req.body)
const {adminstration } = await req.body
const {admin_name, admin_email, admin_password} = adminstration
const newadminstration = await pool.query("INSERT INTO adminstration (admin_name, admin_email, admin_password) VALUES ($1, $2, $3) RETURNING *", [admin_name, admin_email, admin_password]);
return res.json(newadminstration.rows[0]);
}
catch (err) {
console.error(err.message);
}
});
评论
commit