gpt4 book ai didi

javascript - 使用 Mongoose 进行软删除不起作用

转载 作者:行者123 更新时间:2023-12-03 01:49:50 25 4
gpt4 key购买 nike

我正在尝试使用以下 Mongoose 代码软删除项目:

// Remove a client
exports.delete = (req, res) => {
logger.info(`Removing Client ${req.params.clientId}`);
Client.findByIdAndUpdate(
{ _id: req.params.clientId },
{ active: false },
{ new: true }
)
.then(client => {
logger.info('client: ', client);
if (!client) {
return res.sendStatus(404);
}
res.sendStatus(204);
})
.catch(err => {
logger.error(err);
res.status(422).send(err.errors);
});
};

但由于某种原因,active 标志只是不想设置为 false。日志显示 clientId 正在传入,then 代码具有客户端数据,但 active 标志仍设置为 true。我需要以某种方式刷新或提交吗?

编辑 - 添加架构

export const ClientSchema = new Schema(
{
name: {
type: String,
trim: true,
index: true,
unique: true,
required: true,
}
},
{ collection: 'clients' }
);

最佳答案

正如您在评论中所说。

您忘记将 active 字段添加到 mongoose 架构中。

关于javascript - 使用 Mongoose 进行软删除不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50466384/

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