so i was trying to make a function that will sort table that i have by using multiple parameter, so that i can filter the table data, the parameter will be passed as JSON, something like this
{
"availability" : ["mon", "wed"],
"grade" : [A", "B"]
"minAge" : 16,
"maxAge" : 30,
"minPrice" : 10
And this is the line of that code:
exports.searchTutor = async (req, res, next) => {
try {
let {
availability,
grade,
gender,
minAge,
maxAge,
minPrice,
maxPrice,
sortOrder
} = req.body;
if (availability) {
const availabilityArray = Array.isArray(availability)
? availability
: availability.split(',').map(day => day.trim());
availability = availabilityArray;
console.log(availability);
}
const tutors = await TutorProfile.findAll({
where: {
availability:{
[Op.contains]: availability
},
grade: grade
},
});
res.json(tutors);
} catch (error) {
console.error('Error searching tutors:', error);
res.status(500).json({ error: 'An error occurred while searching for tutors' });
}
};
What I wanted to do was that values return the column value if it contains one of the availability values that was specified.
when I changed the query to
availability: availability
And it works, it returns the value when availability matches the specified parameter.
And I also check whether the availability parameter is an array, and it is.
Pratama is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
3