I'm working on a project with Nest.js where I have to make a query object like this with my dynamic filter builder. I encountered a problem where I have to make an object with some depth to send it as part of the query to an external API. All the '2nd level' properties are visible..(the object inside the object), but all the '3rd level' properties are shown as [Object] or [Array].
//my completed filter
const filterArr = [
{ match: { platform: 'INSTAGRAM' } },
{ match: { grade: 'CELL' } },
{ match: { gender: 'FEMALE' } },
{ match: { follower: [Object] } }
]
//my place holding query
let query = {
query: {
bool: {
must: null,
},
},
};
//and I add my filterArr to my place holding query like this:
query.query.bool.must = filterArr;
//and my result is this
{ query: { bool: { must: [Array] } } }
When I try to take that object to make a search query to elasticsearch, it does not let me do it and returns me this:
/// just replaced some private info with ...
[Nest] 261066 - 2023. 06. 19. 오후 11:08:40 ERROR [ExceptionsHandler] Request failed with status code 400
AxiosError: Request failed with status code 400
at settle (/home/.../Desktop/.../.../node_modules/axios/lib/core/settle.js:19:12)
at Unzip.handleStreamEnd (/home/.../Desktop/.../.../node_modules/axios/lib/adapters/http.js:570:11)
at Unzip.emit (node:events:539:35)
at endReadableNT (node:internal/streams/readable:1345:12)
at processTicksAndRejections (node:internal/process/task_queues:83:21)
What I expect to see is
[
{ match: { platform: 'INSTAGRAM' } },
{ match: { grade: 'CELL' } },
{ match: { gender: 'FEMALE' } },
{ match: { follower: { gte: 0, lte: 1000 } }
]
inside my query instead of [Array].
I tried making my query object into string since I use JSON.stringify(query) to send the actual request.
This is the filterBuilder I made
filterBuilder = (
platform?: string,
grade?: string,
gender?: string,
gte?: number,
lte?: number,
) => {
let filterArr = [];
if (platform) {
let block = { match: { platform } };
filterArr.push(block);
}
if (grade) {
let block = { match: { grade } };
filterArr.push(block);
}
if (gender) {
let block = { match: { gender } };
filterArr.push(block);
}
if (gte !== undefined && lte !== undefined) {
let block = { match: { follower: { gte, lte } } };
filterArr.push(block);
}
console.log(filterArr);
return filterArr;
};
I did some searching and I'm not exactly sure about the terminology of my issue.