如何在 Sequelize 的 'findByPk' 语句中同时使用 'include' 和 'attributes'?
How to use both 'include' and 'attributes' in 'findByPk' statement in Sequelize?
如何在 Sequelize 的 'findByPk' 语句中同时使用 'include' 和 'attributes'。这是我的代码:
exports.findOne = (req, res) => {
var id = req.params.id;
User.findByPk(id,
{
include: ["roles"]
},
{
attributes: {
exclude: ['password']
}
}
)
.then(data => {
res.send({"data": data, "resultCode": 1, "message": ""});
})
.catch(err => {
res.status(500).send({
message:
err.message || "Some error occurred while retrieving users."
});
});;
}
但只有当我只使用 2 个中的 1 个时它才有效。
如果我只是使用:attributes: { exclude: ['password']}
结果如下:
{
"data": {
"id": 1,
"username": "admin",
"email": "admin@gmail.com",
"createdAt": "2021-09-27T04:22:56.660Z",
"updatedAt": "2021-09-27T04:22:56.660Z"
},
"resultCode": 1,
"message": ""
}
如果我直接使用:{include: ["roles"]}
那么结果如下
{
"data": {
"id": 1,
"username": "admin",
"email": "admin@gmail.com",
"password": "a$m54ioIwzpZRVC9/HqkHyqezOornjmvT9pDEJcOhHbNcSmLOfw3Sg.",
"createdAt": "2021-09-27T04:22:56.660Z",
"updatedAt": "2021-09-27T04:22:56.660Z",
"roles": [
{
"id": 2,
"name": "moderator",
"createdAt": "2021-09-27T04:20:46.956Z",
"updatedAt": "2021-09-27T04:20:46.956Z",
"user_roles": {
"createdAt": "2021-09-27T04:22:56.791Z",
"updatedAt": "2021-09-27T04:22:56.791Z",
"roleId": 2,
"userId": 1
}
},
{
"id": 3,
"name": "admin",
"createdAt": "2021-09-27T04:20:46.956Z",
"updatedAt": "2021-09-27T04:20:46.956Z",
"user_roles": {
"createdAt": "2021-09-27T04:22:56.791Z",
"updatedAt": "2021-09-27T04:22:56.791Z",
"roleId": 3,
"userId": 1
}
}
]
},
"resultCode": 1,
"message": ""
}
如何在我的代码中同时使用两者?因为我不想在结果中显示 'password' 字段,但我想在结果中显示用户的角色。怎么办呢。提前谢谢你
选项应该像这样在 1 个对象中。
User.findByPk(id,
{
include: ["roles"],
attributes: {
exclude: ['password']
}
})
如何在 Sequelize 的 'findByPk' 语句中同时使用 'include' 和 'attributes'。这是我的代码:
exports.findOne = (req, res) => {
var id = req.params.id;
User.findByPk(id,
{
include: ["roles"]
},
{
attributes: {
exclude: ['password']
}
}
)
.then(data => {
res.send({"data": data, "resultCode": 1, "message": ""});
})
.catch(err => {
res.status(500).send({
message:
err.message || "Some error occurred while retrieving users."
});
});;
}
但只有当我只使用 2 个中的 1 个时它才有效。
如果我只是使用:attributes: { exclude: ['password']}
结果如下:
{
"data": {
"id": 1,
"username": "admin",
"email": "admin@gmail.com",
"createdAt": "2021-09-27T04:22:56.660Z",
"updatedAt": "2021-09-27T04:22:56.660Z"
},
"resultCode": 1,
"message": ""
}
如果我直接使用:{include: ["roles"]}
那么结果如下
{
"data": {
"id": 1,
"username": "admin",
"email": "admin@gmail.com",
"password": "a$m54ioIwzpZRVC9/HqkHyqezOornjmvT9pDEJcOhHbNcSmLOfw3Sg.",
"createdAt": "2021-09-27T04:22:56.660Z",
"updatedAt": "2021-09-27T04:22:56.660Z",
"roles": [
{
"id": 2,
"name": "moderator",
"createdAt": "2021-09-27T04:20:46.956Z",
"updatedAt": "2021-09-27T04:20:46.956Z",
"user_roles": {
"createdAt": "2021-09-27T04:22:56.791Z",
"updatedAt": "2021-09-27T04:22:56.791Z",
"roleId": 2,
"userId": 1
}
},
{
"id": 3,
"name": "admin",
"createdAt": "2021-09-27T04:20:46.956Z",
"updatedAt": "2021-09-27T04:20:46.956Z",
"user_roles": {
"createdAt": "2021-09-27T04:22:56.791Z",
"updatedAt": "2021-09-27T04:22:56.791Z",
"roleId": 3,
"userId": 1
}
}
]
},
"resultCode": 1,
"message": ""
}
如何在我的代码中同时使用两者?因为我不想在结果中显示 'password' 字段,但我想在结果中显示用户的角色。怎么办呢。提前谢谢你
选项应该像这样在 1 个对象中。
User.findByPk(id,
{
include: ["roles"],
attributes: {
exclude: ['password']
}
})