如何使用会话存储中的id通过sequelize添加内容



Hello im currenly new to react js和node js我问我如何只使用已经登录的人的姓名和id插入农场名称(我使用JWT身份验证在会话中存储访问令牌(,也使用sequelize。我也在使用mySQL。

我的目标是将农场名称保存到数据库中。带有登录用户的Id。

因为我稍后会将它们显示给该特定用户。因此,每个添加的场都必须链接到登录的用户id。

这是我的后端:

const router = express.Router();
const { Farm } = require("../models");
router.post("/", async (req, res) => {
const { farmname } = req.body;
const { id } = req.session.user;
const farm = await Farm.create({
farmname,
userId: id,
});
res.json(farm);
});
module.exports = router;

这是react前端中的功能

axios.post(`http://localhost:3001/addfarm`, values).then((response) => {
console.log(response.data);
});
};

这也是我的gerant表:


module.exports = (sequelize, DataTypes) => {
const Farm = sequelize.define("Farm", {
farmId: {
type: DataTypes.INTEGER,
primaryKey: true,
autoIncrement: true,
},
farmname: {
type: DataTypes.STRING,
allowNull: false,
},
gerantId: {
type: DataTypes.INTEGER,
allowNull: false,
},
});
return Farm;
};

这是我的农场餐桌:

const Gerant = sequelize.define("Gerant", {
gerantId: {
type: DataTypes.INTEGER,
primaryKey: true,
autoIncrement: true,
},
phonenumber: {
type: DataTypes.INTEGER,
allowNull: false,
},
password: {
type: DataTypes.STRING,
allowNull: false,
},
});
Gerant.associate = (models) => {
Gerant.hasMany(models.Farm, {
onDelete: "cascade",
foreignKey: "gerantId",
as: "farms",
});
};
return Gerant;
};

你好,

我不明白你的问题,但是。我有一个想法,你想把一个CCD_ 1保存到名称&用户的id。

React中,您可以只使用useState挂钩

示例:

import React, { useState } from "react";
export default function App() {
const [count, setCount] = useState(0);
return (
<div>
<p>You clicked {count} times</p>
<button onClick={() => setCount(count + 1)}>Click me</button>
</div>
);
}

最新更新