gpt4 book ai didi

javascript - 更新数据库后返回更新的对象而不是计数

转载 作者:行者123 更新时间:2023-12-02 21:32:55 25 4
gpt4 key购买 nike

我正在更新食品卡车数据库中的条目。这是我的模型在这次微粒更新中的样子:

function editTruck(changes, id) {
return db('trucks')
.where({ id })
.update(changes)
.then(id => {
return findTruckById(id);
})
}

我想以对象形式返回实际更新的条目。正如您在上面所看到的,我尝试通过利用模型中另一个名为 findTruckById 的方法来完成此任务。 findTruckById 如下所示:

function findTruckById(id) {
return db('trucks')
.select('id', 'name', 'image', 'operator_id', 'cuisine_type', 'physical_address')
.where({ id })
.first()
}

这不起作用,因为从 editTruck 方法返回的响应只是数据库中已更新条目的计数,即 1,而不是 id 或数组条目的 id。因此,它总是只返回 1,并且在 findTruckById 中运行 1 会返回一个与卡车数据库中 id 为 1 的条目等效的对象,这绝对不是我们想要的。我该如何解决这个问题?

最佳答案

您已经拥有 ID,不需要“更新”来返回它。

function editTruck(changes, id) {
return db('trucks')
.where({ id })
.update(changes)
.then(() => { // you already have the id, just use the `id` that is already in the scope
return findTruckById(id);
})
}

关于javascript - 更新数据库后返回更新的对象而不是计数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60574023/

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