幾天來,我一直在努力讓這件事發揮作用,我覺得我有這些東西,但我不知道我錯過了什么。
當我的物件進入我的服務時,它看起來像這樣:
GetAllDataFilterDto {
orgId: 12345,
status: [ 'approved', 'pending' ],
}
我正在使用 typeorm,這是我的查詢:
protected getAll(filter: GetAllDataFilter): SelectQueryBuilder<EntityName> {
const qb = Repository<EntityName>
.createQueryBuilder('entityName')
.select([
'entityName.id',
'entityName.status',
'entityName.orgId',
])
.groupBy('entityName.id')
.where(this.getFilterConditions(filter));
return qb;
}
我的查詢使用 getMany() 但在不同的檔案中,這就是為什么它不包含在此函式中。
這是我遇到問題的地方,我當前的過濾器功能如下所示:
protected getFilterConditions(filter: GetAllDataFilter) {
return _.pickBy(filter, (value, key) => value !== undefined && this.entityProperties.has(key));
}
entityProperties 是一個如下所示的集合:
Set {
'id',
'status',
'orgId'
}
目前,此過濾器適用于數字,如果我的陣列只有一個值,但是當我添加兩個值時,它會回傳錯誤
trace: QueryFailedError: Operand should contain 1 column(s)
我的最終目標是過濾我回傳的資料,即使有可選引數,所以將回傳所有已批準或待處理的狀態列,但我仍然需要能夠同時按 orgId 過濾。
請讓我知道是否還有更多我應該包含的內容,謝謝。
uj5u.com熱心網友回復:
我認為問題可能出.where在您queryBuilder的 1 引數上(換句話說,僅檢查 1 個欄位的條件),然后您可以在另外.andWhere1 個引數上添加添加過濾器。
從檔案:
對于單一.where條件:
添加 WHERE 運算式很簡單:
createQueryBuilder("user").where("user.name = :name", { name: "Timber" })
對于多個.where條件:
您可以將 AND 添加到現有的 WHERE 運算式中:
createQueryBuilder("user") .where("user.firstName = :firstName", { firstName: "Timber" }) .andWhere("user.lastName = :lastName", { lastName: "Saw" })
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/485968.html
標籤:javascript 数组 打字稿 目的 巢穴
