2017년 6월 30일 금요일

FirebaseDB GetValue / GotValue method not reading returned Value correctly


I've noticed an error with FirebaseDB GetValue function or more likely the "when FirebaseDB GotValue" function.

If I structure my firebase database table as follows

root
 - child1
   -- 0: "Hello World"
   -- 1: "Hello World"
   -- 2: "Hello World"

Then ask FirebaseDB to GetValue for tag "child1", the FirebaseDB GotValue then returns a list with all my "Hello World" text string elements

Now if I structure my firebase database table as follows

root
 - child1: "Hello World"

and similarly ask Firebase to GetValue for tag "child1", the FirebaseDB GotValue only returns a Value, which is not interpreted as a list in this case, as "Hello". I also noticed that if I make my child1: "Hello,World" this too just returns "Hello".

--
You probably read Evan's post to your previous question  Can Firebase DB ProjectBucket be an empty string  

How this works probably depends on how you created your table.  Did you use AppInventor or did you create the table and then link it?    What may be happening is your table is providing a JSON and you are attempting to interpret it as a List because you provided the data using the Firebase Console?


If that is not what you are experiencing, then it would be helpful for posting your blocks and providing an 'example'     

Did you create the table using the Firebase Console or using AI2?   How the table is created might explain the problem.  If you create the table using Blocks, does the same thing happen?

--
This is a known issue. As I mentioned previously the content of a field needs to be JSON-encoded for it to be correctly handled by App Inventor. The Firebase UI presents the "" characters but that is just a visual feature for human consumption, App Inventor does not receive the quotes. Therefore, string values you want AppInventor to treat as strings need to be wrapped in quotes. For example, write a string using the Firebase component and then look at the data. You will see something on the web interface like "\"Hello World\"".

When App Inventor attempts to parse the text without quotes from Firebase, it will go as far as it can until it encounters something it doesn't understand. It treats Hello as a token parsed from the JSON stream, but then cannot proceed past the space and gives up, which is why you get the "Hello" but not "World". This is equally true of your inclusion of a comma, since these characters (among many others) terminate JavaScript tokens.

-- 
Ok wasn't sure if this is a known issue. In my opinion, the point of Firebase is to allow collaboration / real time data sharing no matter if data coming from MITdeveloped app or web app or directly from console. So the workaround is not really practical. What you're saying is that the parsing code you've developed so far is not really working as intended and will be fixed sometime soon. Still not sure why an array of strings which are not wrapped in quotes works though. Anyway I'll it up to you guys. Otherwise the firebase extension is working very well and it's a great addition to the suite of components / extensions provided. Well done.

-- 
To solve my problem, I created a json style parser (see attached for procedure) which seems to work on simple firebase structures (not nested trees).

So if firebase structure as follows
root
 - child1
   -- IsAbaby: no I'm not
   -- DoB: Mon 12-Dec-2000 12:00
   -- No: 1232

and then ask to getValue for child1, the GotValue method will return a string like this {IsAbaby=no I'm not,DoB=Mon 12-Dec-2000 12:00,No=1232}

Push this to the procedure and this will return a list with key value pairings.


-- 
Are you aware of the Web component's JSON decoder block?


-- 
Thanks for the tip Abraham. 

Yes I am aware of it, but assumed it was only possible to use if using the web component to retrieve information from web service api's etc. It did not dawn on me to try and use for parsing firebase data. Will look into it.

-- 
I tested the suggested use of web component JsonTextDecode and in my case it returns an error 1105 "unable to decode the JSON text..."

-- 
The spec for JSON is at JSON.org
(for what it's worth).

-- 
Thanks for the spec, but unfortunately I cannot change what the firebase component gives me.

-- 
We'll do some investigation into improving the Firebase component. If I recall correctly the original intent was not necessarily to support arbitrary interaction between App Inventor apps and others via Firebase, but rather app to appinteraction for apps built with App Inventor. That being said, I can see how it would be useful to enable data sharing between arbitrary apps. My immediate guess based on your posts is that the Firebase library is actually returning a JSON object rather than a string (which is what App Inventor currently expects) and so it's just serializing this into whatever default String representation Firebase provides. This is why it is JSON-like, but not really JSON.

-- 

It is not just about sharing data between apps and web-browsers etc. but also allowing data of sharing with IOT sensors though other apis. This experimental component now opens up so many new options for App Inventor application development. 

Then add in the fact that you also can now use Firebase Functions too (just tested my first "hello world" example with App Inventor using the web connectivity component and it works!!!), which then provides you with a very powerful backend for App Inventors. I seriously hope you too can see value in enhancing the existing firebase component functionality as soon as possible. 

The most urgent thing to address for the firebase component is allowing for authentication of users with firebase.

Then as you correctly identified, the firebase output provided as I described is JSON-like. Which is fine for me as can still parse myself. Mind you I'm now looking at Firebase functions to do this for me using a http get request, especially if I have complex tree structures. I then use firebase functions to format the response correctly as a proper JSON object for the App Inventor's web JSON parser to deal with. So we have ways round the problem.

-- 

댓글 없음:

댓글 쓰기