Need to display a nested array as dropdown field's values

Hi! I know this is probably not the smartest way to do it, but I just want to understand if it is possible. Stuck for 4 hours already, so I gave up and ask for help :slight_smile:

Firestore database connected to appgyver, schema looks like this:

These are car brands and models. I’ve got a feed for the Brand dropdown with the following MAP:

MAP(data.Cars1, {label: item.fields.brand.stringValue, value: item.fields.brand.stringValue})

That works fine.

Then for the Model dropdown, I only want to fetch the models, relevant for the respective brand… and that is where I fail to get the list. It gets as good as showing just the first model with the following formula fetching just the [0] element:

MAP(SELECT(data.Cars1,item.fields.brand.stringValue==pageVars.SelectedBrand),{label: item.fields.models.arrayValue.values[0].stringValue ,value: item.fields.models.arrayValue.values[0].stringValue})

everything else shines in red, full of error messages. Do I need a nested MAP? Or a a FOREACH cycle? No idea how that would look like. Help! :slight_smile:

Add the models array property after the select formula.

MAP<models>(SELECT(data.Cars1,item.fields.brand.stringValue==pageVars.SelectedBrand).item.fields.models.arrayValue.values, {label: models, value: models})
1 Like

Thank you Mihaly.
Will try that. An interesting finding also from yesterday is that this db schema made 50k reads in 2 hours yesterday :smile: Re-thinking.

Definitely a good idea to reconsider. :sweat_smile:

1 Like

Can we say it is smarter to keep the data (list of brands & models) as a page variable that gets set @ Page Mount and get firebase db out of the picture?
Give me a hint how to use the json file that holds all the ~600 models so I don’t enter them manually for the variable setup :smiley:

thank you in advance!

Well, I would not keep them in a pageVariable as that would most probably slow down the app.
So use a local data resource. And also the REST api database as a backup service.

So for example load the data from the api on app launched event then set a local data source to the items that have been just fetched from the cloud database. Use the local storage data as you would the rest api data during the regular process in the app. On any updates that you do to the data that is important to keep in the cloud again for future use when you update the record in the cloud database update it with the same result in the local storage so that You keep consistent data.
If there is a concern that a user doesn’t save the data manually you can add a warning on page will unmount event and add an auto-save (update records) functionality there.

As for the hint. Honestly just repeat the rest api data structure in the local data resource. The only thing that is important to keep in mind is to add a “cloud_id” (this is just a sample name) property to the local storage so that you can reference the data in the cloud rest api to the one in the local storage.

I used jsonbin.io for rest api with the same json file:

slightly modified your formula to:

MAP(SELECT(data.Testis1, item.brand==pageVars.SelectedBrand),{value:id.models,label:id.models})’’’

but I end up with a single line line in the dropdown listing all models:

: ( How?!

You MAP() through the wrong array.
What You do is to select the Car brand and then assign ALL of its models to the value and label.
Also what is “id”?? Where is that created?
You have to MAP() through the selected brand’s models. And have a different name for the two arrays… Because item refers in Your formula to the data.Testis1. So you cannot use that. But in the MAP<>() you didn’t add any name, so it wants to use “item” again. But it cannot even try to use “item” again, because it has “id”, which I cannot see declared anywhere…

So this:

MAP<models>(SELECT(data.Testis1,  item.brand==pageVars.SelectedBrand).models, {value: models, label: models})

Thanks for your patience, master :slight_smile:
The ID came through the schema setup as on screenshot in my previous post… which I apparently need to change.

that is what the json looks like:

[
{“brand”: “Seat”, “models”: [“Alhambra”, “Altea”, “etc”] },
{“brand”: “Renault”, “models”: [“Captur”, “Clio”, “etc”] }
]

Schema was set to “text” for “brand” (which worked), and then “List of object with 1 property” for Models… hence the id, which apparently is setup when you change the property type to “object”.

So for the formula provided by you to work, what should be the types of the two properties Testing all types and never get it to work.

Sorry for late replies, just want to put some thought in it before replying.

1 Like

Try using FIND() instead of SELECT. Like really just change the SELECT tot FIND the formula properties are the same. Because I have overlooked the fact that You need one single item from the json and have that’s models properties.
SELECT formula returns a list.
So two solutions:

MAP<models>(SELECT(data.Testis1,  item.brand==pageVars.SelectedBrand)[0].models, {value: models, label: models})

Or:

MAP<models>(FIND(data.Testis1,  item.brand==pageVars.SelectedBrand).models, {value: models, label: models})
1 Like