我最喜欢的标签按钮有点问题.我不能发送邮件,我不知道如何解决它……以下是我的前端代码:
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;
我也有续集连接和这个:
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}!`))
有人知道我的错误会发生什么吗?
我try 了不同的方法,但我无法更改数据库上的布尔值...