Hello,Apologies if this isnt the right place for this. I'm posting here because I saw some sequelize implementation posts show up on Google.Basically, I have 2 models, Recipe and Ingredient. They have a belongsToMany relationship, so they are linked to each other in a through table.I'm passing an ingredients query through a url query. If I'm querying ['tomato'], it returns all the recipes that requires tomato. The problem is when I'm querying more than one ['tomato','potato'].Here is a visual through table of what I'm trying to accomplish.RecipeId | IngredientId ------------------- 1 | Tomato 1 | Potato 2 | Broccoli 3 | Tomato 3 | Potato If I'm querying ['tomato', 'potato'], I should get RecipeID 1 and 3. But I'm not sure how to accomplish that.This is what I attempted, which only works for 1 ingredient.I'd appreciate any input I could get. Thanks in advance!const { ingredients } = req.query; // { ingredients: tomato,potato } const parseIngredients = ingredients.split(',').map((ingredient) => { return { name: { [Op.iLike]: `%${ingredient}%`, }, }; }); const recipes = await Recipe.findAll({ include: { model: Ingredient, where: parseIngredients, }, });
Submitted September 10, 2020 at 03:44PM by giantqtipz
No comments:
Post a Comment