问题
I'm using the strapi framework based on node.js and while creating a query-builder, I'm facing an issue with the andWhere() clause.
I am implementing search functionality where a user can input some text in a form input field, which gets passed in, and database table fields are queried based on that text.
In the code below, you can see that I query for two things:
- Search the product model wherever the text is present in the name field.
- And always check and search the product model wherever the company field contains the value stored in usercompany variable - let us assume to be "Apple".
OK, so basically I have multiple user roles and I want to add another condition to this.
I'm trying to use the dot (.) method to access nested relations in a query, but this doesn't seem to work:
qb.andWhere('createdby.role', role)
where the role will be the value of the current logged in user, a numerical field where each role id is mapped to some string value like 'Admin' or something in the database already.
However, this results into an error that doesn't allow me to query nested relations. I don't have much experience with bookshelf so IDK how to solve this.
Code:
var test = await strapi
.query("product")
.model.query((qb) => {
qb.where(function () {
this.where("name", "LIKE", "%" + searchterm + "%");
});
qb.andWhere("company", usercompany);
qb.andWhere("createdby.role", role); // Here, role = 8
})
.orderBy("updated_at", "desc")
.fetchAll();
Error:
error Error: Undefined binding(s) detected when compiling SELECT. Undefined column(s): [createdby] query: select `product`.* from `product` where (`name`
like ?) and `company` = ? and `createdby` = ? order by `product`.`updated_at` desc
Hoping for a reply as soon as possible, thanks!
来源:https://stackoverflow.com/questions/63243138/bookshelf-js-nested-relations-while-querying-with-andwhere