Parsing JavaScript object variable



Peter Bradstreet

Hi David,

I hope that you are well. I am hitting a bit of a brick wall on something that I sense should be deady easy and I am wondering if you could give me a quick hand. Throughout my app in AB1 I have firebase firestore queries that pull back query results and I populate report objects with the response data. To edit individual records, I pick up the onclick event and edit each record in an edit view and all is good.

Now I have a couple of views where the data returned is for a single record such as in the case of a user profile view. I want to pull in the single record when the view is loaded and populate several fields in the view such as firstname, lastname and email. I have the following code running on view show:

This properly pulls in the record and shows the following in the console:

I think that I need to parse this object in order to obtain the individual field values in order to place them into the view for editing but I am struggling with the proper approach. If you could point me in the right direction it would be greatly appreciated.

cheers, Pete

David Esperalta

Hello Peter,

I think you are doing well. Once you get "doc.data()", you can simply place that object's properties where you wanted. For example, in a way like this:

Something like the above can work. Even you can avoid the "userData" local variable and just access the "doc.data()" like "doc.data().firstname". Just like you prefer.



Peter Bradstreet

Ah, that is perfect, it worked. I wasn't declaring the userData variable and that was my problem.

Many thanks as always! Pete

David Esperalta

No problem, Peter. In fact the "userData" variable is just may convenient, but, you can also directly use "doc.data()" as an object. In both ways we are talking about a JavaScript object which contains the properties that you wanted.



Peter Bradstreet

You are right, I just tried it. I was trying to put the field in the brackets 'doc.data(firstname)', not 'doc.data().firstname' as I should have been. I figured I was making a dumb mistake and I was.

Cheers, Pete

David Esperalta

Well, all days we can learn something. :-)

In fact, you labelled this thread like "Parse Array variable". What probably happen is that "doc.data()" sometimes stores a JavaScript Array, and sometimes other type, like in this case, a JavaScript object. So you simple try to access the stored value as an Array, and, of course, that can't work.

Just remember the console output: the output is a piece of JavaScript too, so, if that output starts by a square bracket, we are talking about a JavaScript Array. On the other hand, if, like in this case, the output starts by a curly bracket, we are talking about a JavaScript Object.



Everybody can read the DecSoft support forum for learning purposes, however only DecSoft customers can post new threads. Purchase one or more licenses of some DecSoft products in order to give this and other benefits.