Firestore and List of Objects

Hi, here’s a working example! items is an array, and the items are of type map.

In Composer, the schema for Products is:

type: text
items: list of objects with properties manufacturer: text and price: number

I created a “Collection of records” type data variable from the Firestore resource called Products, and I’m using the ENCODE_JSON() formula here to show it as a string in a paragraph component:

image

Hope this helps you get forward!

1 Like

Hi Mari,
thanks for the explanation and the screenshot from firestore.

Still the part about retrieving the data into AppGyver is to fast for me :wink:

  • when you say “I created a list type data variable called …”

a) composer allows me to create e.g. a data varialbe ‘Collection of data records’, but this is not a list


b) composer also allows me to create a page variable with value type ‘list’ , but this is not a data variable.

I am confused about how to bring these two parts together :hushed:

1 Like

Hi, sorry I meant the “Collection of records” type :slight_smile:

2 Likes

Hey @Mari - Thank you so much! Will give it a try asap :slight_smile:

Michael

1 Like

Hello again @Mari

I have tried to walk through your explanation - but I have to admit that I simply don’t get it… Sorry. I am trying hard here to understand what your thoughts were… any chance you can fill in some more screenshots?

Another thing is, that this seems to retrieve the data, which I already managed to do… My real issue is when I try to write / update the records.
Any thoughts on that?

Michael

1 Like

Hi, that’s ok! If you are unfamiliar with using data in Composer, I’d start from watching these videos.

Here’s how I would create a new record with the example setup:

Here I’m just binding it to a static value {type: "mouse", items: [{manufacturer: "Dell", price: 30}]}

When I click the “Create record” button in the web preview and refresh the page, “Products” has updated to include the mouse type:

Similarly, I can update the records using the “Update record” flow:

I have put in the ID of the record I just created as identifier, and set the record to {type: "mouse", items: [{manufacturer: "Dell", price: 50}]}

Again, the data changes accordingly:

This example is a bit dumb in the way that it uses static value bindings, but you can make use of variable bindings in your app to get the data from for example user input in a form. :slight_smile:

Hi @Mari,

Thank you for elaborating. However, it still does not work for me…

Not sure what you mean by:

it looks like you bind the value through the binding editor…(I can’t see a way to use a static value here)

This is mine:

this is my list of values:

When I push the button, I get this error (same as in my initial post):

As you can see above / behind my error message I do get the data with the ENCODE_JSON-way.

What am I doing wrong here… I am going crazy :wink:

Michael

How are you connecting to Firebase? Are you using the connector as instructed in this video?

Hi, I watched the videos and even the “working with objects” video but I can’t find were can I set this items: list of objects with properties in the schema, can you please tell me were I can set this?
Thank you

HI @Maxwell_Swamy , first, create the items property, then set its type to “List” and then the “List item type” to “Object” – then you can add the object properties.

1 Like

Thank you it worked,
Is there a way to get this data as a list of separated rows for every item inside there? to show it clear to the app user?

Thanks in advance.

Is there any way to sort the data variables in descending order?

Hi @Mari. I recreated your example. But can’t get it to work. I can update the “type” field just fine, but can’t do it with array of maps. Would you please take a look at my screenshots?





I also tried building the object manually but had no success.

Hi Mari,

I have had a look at this and in web preview my firebase create record works but as soon as I do it in the app preview it doesn’t work on my iphone. Is there a dependance that I am missing because teh error I get is “ReferenceError:Cant find Variable: Buffer”

Thanks for the detailed explanation @Mari
Works fine, I can create record but the problem is bellow, red marked portion.

Whenever I press record button, it create new document record with item index “0” always, Not with index 1,2,3… I want like in the pic above. Whenever a same user create record it should always goes under the same item with new index number, not a new document.

Secondly…

I manually create a record on my firebase database like shown above and try to get the item to my preview app by binding, there also I can bind only the type: to my value field by repeating the item. But when I try to bind the item: (list of object with two properties) to one text and a number field… I says “incompatible”… then how can I retrieve that data to my app… pic bellow.

In simple, a user can favorite some items from the list of items, that should store under his documents( I can create the document ID unique for each signed in user, fine for me).

Questions as a newbie…

  1. How can I to create multiple record under items with indexes 0,1,2,3… explained above.
  2. If first question is solved then how to retrieve that to app.

Hopefully get my answer from you.
Bravo

I think @Mihaly_Toth @Cecilia @F_ORTELL can give some good solution to this…

Man, there is a huge misunderstanding and lack of database structure in this case…

If You want to build a list of products You have two options:

  1. Create a new document for each product if the list is to be increased in the future. (with properties like type, manufacturer, price, etc.)
  2. Create 1 document with a list of products with different properties as You did above. Now, this causes some issues in the data retrieval as You have to understand how data is structured pretty well to use that.

For the first question, Your solution would be binding in the “Update record” logic flow to “Object with custom properties”. Why update the record? Because You already have that and want to add a new item into that. So inside the “Object with properties” you will need to set the items property to a formula something like this:

WITH_ITEM(data.yourCategoryDocument.items, pageVars.newItemToAdd)

Make sure to change the formula inputs to your variables…

For the second question… You try to repeat a text with the “documents” list. But Your document has another list with which You should repeat the text. So what You would need to do is to change the “Repeat with” property of the text 7 component to the data.yourCategoryDocument.items

I guess that is all I can help with. Please learn a little more about data structures to avoid issues like these. The time spent on that will pay off. :pray:

Thanks for your time & detailed explanations @Mihaly_Toth

But the above formula binding not works for me. I’ll screenshot bellow for better understanding. Now I moved little further by searching many articles, that now I can retrieve the data to my app by nested repeating. That’s fine, I can update the particular value if I use the curresponding document ID by using pageVars binded to my “object with properties” custom list “items”.

Still the main problem exist until now…
I want to add/update a particular item(list of object with properties) inside the arrayValue by using conditions(if the document id is same/if the same object exist add a new one like so) as explained above. Am totally lost here…

This is what it says when I try with the formula that you said. There is may orange objection messages appears. Even I save and try it wont work. The formula binded property shows “null”.

If I use pageVars to bind instead of your formula it update the record according to the id that I provided. Even though, I cannot apply conditions to add another object list(like 0,1,2,…)

Hoping the best…

Well, you try to add an item that is not an object inside a list of objects… Also You have a list of objects and a list of objects. And You want to add one object from the second list of objects? I mean I am also lost there. Why would you want to do that?
Your page variable “Products” is a list of objects. Your data variable Products is a list of objects. What is the goal that You want to do?

What is the difference between pageVars.Products and data.Products.items???

Another note. If You have the price as a number and You assign its value from an input field I suggest using a NUMBER() formula as the input field may return the number as a string. This can also cause a few problems when saving data.

In my App, there is an option where the signed in user can add an item to his wallet from a category… If he add another item from the same category it should come under that category in his account… and the whole item should show up in the page as repeated items.

I can do repeat the list and repeat another list under one list, tested work fine. But I am confused and totally lost in how to check the list of object and add another “objects with property” to the same list of objects(In above case the list of object is “items”).

No, I tried that with numbers and strings