How do I filter an http-function query by a reference field? I have an employee table and a company table. I want to filter the employee table based on the company reference field value. As the function name implies, I want to get Employees based on the Company ID parameter I provide.
export function get_getEmployeesByCompanyId(request) {
const options = {"headers": { "Content-Type": "application/json"}};
if (!request.query.company_id){
throw new Error('missing "company_id"');
}
console.log("company_id: " + request.query.company_id);
return wixData.query("Employees")
.eq('company',request.query.company_id)
.find()
.then(results => {
if (results.items.length >0 ){
options.body = {
"items": results.items
}
return ok(options);
}
})
}
I have tried using the company’s ID value, and I’ve tried using the company name. Both of these threw internal server errors.
The WiX webpage editor can do this when you have a collection open in the Dashboard by clicking on
→ [ Filter ]
→ [ + New Filter ]
→ Select Field = “[(-)] Company”
→ Choose a Condition = “Contains some”
→ Enter a Value = “My Company Name”
→ [ Add Filter]
I am creating an API using http-functions, so I don’t have a webpage dataset to operate on.
Thanks!