[英]Sequelize not updating a row in table
I am using sequelize for the first time.我是第一次使用续集。 I have a table called
feed
in my DB, with columns ID
, caption
, and url
.我的数据库中有一个名为
feed
的表,其中包含列ID
、 caption
和url
。 In my controller I have a simple function where I am updating a row in a table:在我的 controller 我有一个简单的 function 我正在更新表中的一行:
router.patch('/:id',
requireAuth,
async (req: Request, res: Response) => {
let { id } = req.params;
const item = await FeedItem.update({ ...req.body }, {
where: {
id: id
}
});
if (item === null) {
return res.status(404).send({ message: 'Feed not found' });
}
res.send(item);
});
I am sending json body that looks like this:我正在发送看起来像这样的 json 主体:
{
"caption": "NewTest",
"url": "Newtest.jpg"
}
I see that data is sent correctly in the console, but when I post to the endpoint with an id of 2, I get as a response:我看到数据在控制台中正确发送,但是当我发布到 id 为 2 的端点时,我得到了响应:
[
1
]
I can also see in the DB, that the row has not been updated.我还可以在数据库中看到该行尚未更新。 What am I doing wrong here, how can I updated a row in the table and return the new updated value?
我在这里做错了什么,如何更新表中的一行并返回新的更新值?
FeedItem.update
method returns count of affected rows. FeedItem.update
方法返回受影响的行数。 That is why you are getting 1
.这就是为什么你得到
1
。 If you need to have FeedItem
instance itself you can retrieve it first and update the instance:如果您需要
FeedItem
实例本身,您可以先检索它并更新实例:
let feedItem = await FeedItem.findById(id);
if (!feedItem) {
return res.status(404).send({ message: 'Feed not found' });
}
feedItem = await feedItem.updateAttributes(req.body);
return res.send(feedItem);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.