gpt4 book ai didi

javascript - 在 express 中通过表格发布数据以进行 Sequelize

转载 作者:行者123 更新时间:2023-12-03 22:37:38 24 4
gpt4 key购买 nike

我有一个允许用户互相关注的 postgresql 数据库设置,我使用了一个直通表来设置它。

    as: 'follower',
through: 'follow',
foreignKey: 'follower_id'
})

User.belongsToMany(User, {
as: 'following',
through: 'follow',
foreignKey: 'following_id'
})

table :
| created_at | updated_at | follower_id | following_id |
|------------|------------|-------------|--------------|
| | | | |

我的获取请求:
userRouter.get('/:user_id/followers', async (req, res) => {
try {
const user = await User.findOne({
where: {
id: req.params.user_id
},
include: [
{
model: User,
as: 'follower',
through: { attributes: [] }
}
]
})
res.send(user)
} catch (error) {
console.log(error)
}
})

结果:
{
"id": 1,
"firstName": "John",
"lastName": "Smith",
"username": "tester",
"profileImage": null,
"email": "tester@mail.com",
"password": "$2b$12$h2rVqmxgsGxTOVgq3aII5uCuRcSN0TZZ6lwZgRRHCaOhu98K3AKbe",
"skills": [
"React",
"Python",
"Mongo"
],
"createdAt": "2019-07-07T23:38:07.730Z",
"updatedAt": "2019-07-07T23:38:07.730Z",
"follower": []
}

现在我被困在如何使用 post 或 put 请求向该表添加新条目。

最佳答案

经过一番折腾,我找到了答案。

当我发布一个新的关注者时遇到了一个问题,它覆盖了现有的条目。

这是我设置关联的方法:

const Follower = db.define('follower', {
follower_id: {
primaryKey: true,
type: Sequelize.INTEGER
}
})

Follower.belongsTo(User, {
as: 'user',
through: 'follower',
foreignKey: 'follower_id'
})

User.hasMany(Follower, { as: 'followers' })

我的获取路线:
userRouter.get('/:user_id/followers', async (req, res) => {
try {
const user = await User.findOne({
where: {
id: req.params.user_id
},
include: [
{
model: Follower,
as: 'followers',
include: [
{
model: User,
as: 'user'
}
]
}
]
})
res.send(user)
} catch (error) {
throw error
}
})

邮寄路线:
userRouter.post('/:user_id/follow/:follower_id', async (req, res) => {
try {
const user = await User.findByPk(req.params.user_id)
if (user) {
if (user.id.toString() === req.params.follower_id) {
res.status(400).json({ err: 'You cannot follow yourself' })
} else {
const following = await Follower.findOrCreate({
where: {
userId: req.params.user_id,
follower_id: req.params.follower_id
}
})
res.send(following)
}
}
} catch (error) {
throw error
}
})

现在就像一个魅力:
 follower_id |         created_at         |         updated_at         | user_id 
-------------+----------------------------+----------------------------+---------
2 | 2019-07-07 23:36:06.696-04 | 2019-07-07 23:36:06.696-04 | 1
3 | 2019-07-07 23:36:22.498-04 | 2019-07-07 23:36:22.498-04 | 1

来自api的数据:
{
"id": 1,
"firstName": "John",
"lastName": "Smith",
"username": "tester",
"profileImage": null,
"email": "tester@mail.com",
"password": "$2b$12$0Ir/1zWRPvx3MWRjq6l85.z35QyCPwDqX7TyhAeWZHLx75RmGzlKS",
"skills": [
"React",
"Python",
"Mongo"
],
"createdAt": "2019-07-08T03:36:00.262Z",
"updatedAt": "2019-07-08T03:36:00.262Z",
"followers": [
{
"follower_id": 2,
"createdAt": "2019-07-08T03:36:06.696Z",
"updatedAt": "2019-07-08T03:36:06.696Z",
"userId": 1,
"user": {
"id": 2,
"firstName": "Jane",
"lastName": "Doe",
"username": "test",
"profileImage": null,
"email": "test@mail.com",
"password": "$2b$12$pxg9ssYtSIZm3TPcYDzjnOZApaFjXWPypM8et1m5nSPZeH1voGVHO",
"skills": [
"HTML",
"Css",
"React"
],
"createdAt": "2019-07-08T03:36:00.519Z",
"updatedAt": "2019-07-08T03:36:00.519Z"
}
},
{
"follower_id": 3,
"createdAt": "2019-07-08T03:36:22.498Z",
"updatedAt": "2019-07-08T03:36:22.498Z",
"userId": 1,
"user": {
"id": 3,
"firstName": "Jackie",
"lastName": "Legs",
"username": "test3",
"profileImage": null,
"email": "test3@mail.com",
"password": "$2b$12$V0UgOVxB.c5/gbuUIqvvW.cnWqZjsqLUnHCgoT4zwzFtBPucgRAl2",
"skills": [
"HTML",
"Css"
],
"createdAt": "2019-07-08T03:36:00.758Z",
"updatedAt": "2019-07-08T03:36:00.758Z"
}
}
]
}

关于javascript - 在 express 中通过表格发布数据以进行 Sequelize ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56927060/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com