在react的.map中尝试收藏夹选项卡.我不能改变mysql表的布尔类型



我的fav'选项卡按钮有问题。我不能发送。帖子,我不知道如何解决它…这是我的前端代码:

import React, { useState, useEffect } from "react";
import { Link, useNavigate, useLocation } from "react-router-dom";
import axios from 'axios';
import { Buffer } from "buffer";
function CompoNuevos() {
const { state = { success: false } } = useLocation();
const [cnuevos, setCnuevos] = useState([]);
(...)
useEffect(() => {
fetch("http://localhost:3001/api/cnu")
.then((x) => x.json())
.then((y) => y.data)
.then((allCnuevos) => setCnuevos(allCnuevos));
}, []);
async function handleClick() {
await axios.post('http://localhost:3001/api/oportunidades', {
id: cnuevos.id,
ofertas: true,
});
}
return (
(...)
{cnuevos &&
cnuevos.map((cnuevo) => {
return (
(...)
<button onClick={handleClick}>Marcar como oferta</button>
);
})}
export default CompoNuevos;

这里是我的路线:

import express from "express";
import { DataTypes } from "sequelize";
import sequelize from "../models/loadSequalize.js";
import { Sequelize } from "sequelize";
const Oferta = sequelize.define(
"Oferta",
{
id: {
type: DataTypes.INTEGER,
primaryKey: true,
},
nombre: {
type: Sequelize.STRING,
allowNull: false,
},
precio: {
type: Sequelize.INTEGER,
allowNull: false,
},
descripcion: {
type: Sequelize.STRING,
allowNull: false,
},
data: {
type: Sequelize.BLOB("long"),
allowNull: false,
},
ofertas: {
type: Sequelize.BOOLEAN,
allowNull: false,
defaultValue: false,
},
},
{ tableName: "cnuis", timestamps: false }
);
const oportunidadesRouter = express.Router();
oportunidadesRouter.post("/", async (req, res) => {
const { id, ofertas } = req.body;
await Oferta.update(
{ ofertas: ofertas },
{
where: {
id: id,
},
}
);
res.send({ success: true });
});
export default oportunidadesRouter;

我也有sequelize连接和这个:

import express from "express";
import cors from "cors";
(...)
import oportunidadesRouter from './routes/oportunidades.js';
const app = express();
app.use(express.json());
(...)
app.use('/api/oportunidades', oportunidadesRouter);
const port = 3001
app.listen(port, () => console.log(`App listening on port ${port}!`))

有人知道我的错误发生了什么吗?

我尝试了不同的方法,但我不能改变数据库上的布尔值…

由于cnuevos是一个数组,cnuevos.id将返回undefined(后端逻辑将失败)。您需要将.map内部项目的id作为参数传递给函数handleClick,并在后端端点

上使用此id

相关内容

  • 没有找到相关文章