首页 > 解决方案 > 如何通过ID从数据库中获取对象

问题描述

就我而言,我在我的请求中接受了一个 id 列表,例如,如何使用and[1,2,3]仅从我的数据库中获取具有此 id 的对象?typeormquerybuilder

我试试这个:

if(dto.customersIds){
    Array.prototype.forEach.call(dto.customersIds, ids => {
        res.where(`customer.id = ${ids}`);
    })
}

但这不起作用,让我只得到数组中最后一个 id 的对象。

有人可以告诉我如何通过 id 获取对象吗?

谢谢你的帮助

标签: javascriptdatabasetypescripttypeorm

解决方案


代替:

Array.prototype.forEach.call(dto.customersIds, ids => {
    res.where(`customer.id = ${ids}`);
})

和:

res.whereInIds(dto.customersIds);

我假设res是从createQueryBuilder


推荐阅读