So, I’m creating a recipe search where a person can filter my recipes based on the ingredients. This is a many-to-many relationship, in that a recipe has lots of ingredients and an ingredient can be present in more than one recipe. An abbreviated data diagram is below.
I’ve added the ingredient dropdown filter (dropdown1) to my page, and the recipe repeater (repeater1). The challenge I’m having is that I’m struggling to get the code to work for the nested query. What should happen is that when the user selects an ingredient from the dropdown field, only recipes with that ingredient will be displayed. Here’s what I have
import wixData from ‘wix-data’;
let selectedingredient = “”;
let selectedrecipe = “”;
export function dropdown1_change(event, $w) {
wixData.query(“Ingredient_Tbl”)
.eq(“Title”,$w(“#dropdown1”).value)
.find()
.then((results) => {
let firstitem = results.items[0];
selectedingredient = firstitem._id;
export function dropdown1_change(event, $w) {
wixData.query("Ingredient_Tbl")
//the field key is "title", not "Title"
.eq("Title",$w("#dropdown1").value)
.find()
.then((results) => {
let firstitem = results.items[0];
selectedingredient = firstitem._id;
wixData.query("IngredientRecipe_Tbl")
.eq("ingredient",selectedingredient)
.find()
.then((results) => {
//here it should also be results.items[0];
let seconditem = results[0]
selectedrecipe = seconditem._id;
wixData.query("Recipe_Tbl")
.eq("title",selectedrecipe)
.find()
.then((result) => {
$w("#repeater1").data = result.items;
})
})
.catch( (err) => {
let errorMsg = err;
});
});
}
I recommend checking out also this article to learn more about debugging your site’s code.
Should the issue persists, please send us the site URL and the page name so that we can have a look.
So, it appears Wix doesn’t use relational databases. I go this working using the following workaround.
Create a Database collection with items that you want to appear in your dropdowns (e.g. I want to filter gemstones by colour, origin and typical transparency). Populate this list and ensure you include an option *All
For your item (gemstones) collection, ensure you have a text column that matches each of the filters (e.g. GemstoneColour, GemstoneOrigin, GemstoneClarity).
In these columns, list all the relevant information and make sure you include *All
On page, add your filter boxes (named colourselect, clarityselect and countryselect) and include the following code:
Link this code to the on_change function for all your dropdowns.
Now you only need to change the text in the database collection fields and it will automatically change which filters they appear under.
Notice, I’ve also added a Show/Hide field to each stone to make it easy to manage what people see.
Now your users can easily filter what they see. A great option for letting people filter a portfolio by a style or subject, or recipes by meal or ingredient.
Can you help me, what can be the problem? I see the filters on the page, but when I select one filter for a selection with no result and than back to a selection with valid results is show no items. Thanks. Ildiko
Thank you for your code suggestion. I have implemented it on my site, but unfortunately it doesn’t work. Can you help me? I have placed my code in the below comment. Thanks. Ildiko
Hi,
This is an old thread. Can you please post a new post with your question? Moreover, a video of the issue can be helpful too. You can use screencast for that purpose. Simply recreate the issue while recording the steps add the screencast.com URL to your response.