Skip to content
This repository has been archived by the owner on Apr 4, 2023. It is now read-only.

Can't read number field where value is greater than 0 #186

Closed
fsandreau opened this issue Oct 19, 2016 · 16 comments
Closed

Can't read number field where value is greater than 0 #186

fsandreau opened this issue Oct 19, 2016 · 16 comments
Assignees
Milestone

Comments

@fsandreau
Copy link

fsandreau commented Oct 19, 2016

For some reasons, when I do a query request to get my objects, some properties of the json objects are missed. Below the query :

        data => {
            if(data.value) {
                Object.keys(data.value).forEach(key => {
                    this.results.push(data.value[key]); // Here I dont get exactly the JSON, some properties disapears.....
                }
            }
        }, "/ads/", 
        { orderBy: { type: firebase.QueryOrderByType.KEY }})
        .then(result => { return result; })
    );

Properties of type number are not loaded (totalProduct and state) when value is greater than 0. capture d ecran 2016-10-19 a 19 02 40

Regards

@jlooper
Copy link
Contributor

jlooper commented Dec 6, 2016

I am experiencing this issue as well. This is really bizarre. I can retrieve strings, not numbers. Should I convert everything to a string? This is crazy. In the example below, everything does load the first time, but then on subsequent loads of the app or when I go to a detail page, anything that is a number is omitted entirely. For example, I need all this data back:

screenshot 2016-12-05 21 57 11

but what I get is only that data that is a string.

@EddyVerbruggen
Copy link
Owner

EddyVerbruggen commented Dec 6, 2016

iOS/Android/both?

I've tested on iOS 10 and Android 7 devices with the demo app as well as the query of @fsandreau and everything works fine with my data structure:

screen shot 2016-12-06 at 11 10 14

Whenever I change fi. the age property the data is reflected correctly in my app. Any idea how this is different from your usages?

Can you guys add console.log("Query result: " + JSON.stringify(result)); as the first line in the onValueEvent callback function?

@jlooper If that doesn't shed any new light on this issue then please paste your query code here as well as a minimal export of your db:

screen shot 2016-12-06 at 11 13 59

@jlooper
Copy link
Contributor

jlooper commented Dec 6, 2016

hi, the very consistently bizarre behavior is that, on initial creation of the app I get the expected data:

[{"id":"-KWdk_ax11wUci2ffQra","PracticesCompleted":5,"Reward":"A special prize!","UID":"ade4AX13CLOlBgW1u1hckPZ7Da02","Date":-1479243094507,"Instrument":4,"AdminPassword":"","TeacherEmail":"user@nativescript.org","Name":"Ellen","PracticeLength":5,"PracticesRequired":6}]

and then, if I livesync or just close and reopen the app on device, I get only the strings:

[{"id":"-KWdk_ax11wUci2ffQra","Reward":"A special prize!","UID":"ade4AX13CLOlBgW1u1hckPZ7Da02","AdminPassword":"","TeacherEmail":"user@nativescript.org","Name":"Ellen"}]

@EddyVerbruggen
Copy link
Owner

@jlooper can I clone some repo to debug this? Also, which platform(s)?

@jlooper
Copy link
Contributor

jlooper commented Dec 6, 2016

Sorry, it's iOS. I have my repo here: https://github.com/jlooper/practicebuddy-web-mobile

@EddyVerbruggen
Copy link
Owner

Cool, can you point me at the problematic code as well?

And does Android behave correctly?

@jlooper
Copy link
Contributor

jlooper commented Dec 6, 2016

thanks for your help. The query to get student data is here: https://github.com/jlooper/practicebuddy-web-mobile/blob/master/src/client/app/frameworks/practicebuddy/services/firebase.service.ts#L152-L195

Genymotion is throwing errors all over so I'm battling Android right now. For the moment, though, maybe the above code makes some sense?

@EddyVerbruggen
Copy link
Owner

Ah, so you're not using query (@fsandreau does), but addValueEventListener. I'll try to look at it later today.

@jlooper
Copy link
Contributor

jlooper commented Dec 6, 2016

I've created a sample using both query and adding an event listener. Consistently, when I run the app using tns run ios I get my data back, but when I use tns livesync ios --watch nothing is returned. I would just say, ok, don't use livesync, but I am not convinced that this same problem doesn't occur when I build to device (tns deploy ios --device device-id), close the app, and then reopen it. Here's my repo: https://github.com/jlooper/test-tapper

@EddyVerbruggen
Copy link
Owner

@fsandreau Does your issue also only pop up with livesync (on iOS)?

@EddyVerbruggen
Copy link
Owner

@jlooper Seeing similar things. Before I jump to conclusions: can you be very clear (cold hard facts) to me about livesync vs a 'normal' run? Does the problem only pop up when using livesync?

@EddyVerbruggen
Copy link
Owner

@jlooper changing persist to false in main.ts of your test app seems to fix it. So there may be an incompatibility between Firebase persist-mode and livesync.

@jlooper
Copy link
Contributor

jlooper commented Dec 6, 2016

holy cow. That seems to have done it! Wow, what a strange behavior!! But thank you @eddy and @fsandreau I hope this might help you!!

@fsandreau
Copy link
Author

fsandreau commented Dec 17, 2016 via email

@andreacappadona17
Copy link

andreacappadona17 commented May 4, 2017

Hi, I'm having the same problem when running my app on iOS (iPad 9,7 2017), both in livesync and deploy mode, with persist set to true.

Has anyone managed to make their apps run without errors on iOS while keeping persist to true?
Really need to make apps work offline too.

EDIT:

Ok, debugged a bit the code for the plugin, and it seems like I managed to make the app run correctly, by changing firebase.toJsObject in firebase.ios.js, adding the case for NSDecimalNumber.
It seems like when in persist mode, every number different from zero is treated as NSDecimalNumber and not Number.

Here is the code for function. If it works for everyone, maybe @EddyVerbruggen could update the plugin.


firebase.toJsObject = function(objCObj) {
  if (objCObj === null || typeof objCObj != "object") {
    return objCObj;
  }
  var node, key, i, l,
      oKeyArr = objCObj.allKeys;

  if (oKeyArr === undefined) {
    // array
    node = [];
    for (i = 0, l = objCObj.count; i < l; i++) {
      key = objCObj.objectAtIndex(i);
      node.push(firebase.toJsObject(key));
    }
  } else {
    // object
    node = {};
    for (i = 0, l = oKeyArr.count; i < l; i++) {
      key = oKeyArr.objectAtIndex(i);
      var val = objCObj.valueForKey(key);
      switch (types.getClass(val)) {
        case 'NSArray':
        case 'NSMutableArray':
          node[key] = firebase.toJsObject(val);
          break;
        case 'NSDictionary':
        case 'NSMutableDictionary':
          node[key] = firebase.toJsObject(val);
          break;
        case 'String':
          node[key] = String(val);
          break;
        case 'Boolean':
          node[key] = val;
          break;
        case 'Number':
        case 'NSDecimalNumber':
          node[key] = Number(String(val));
          break; 
      }
    }
  }
  return node;
};

@EddyVerbruggen
Copy link
Owner

Fixed in 4.0.0 thanks to issue #365 (and in retrospect the code posted by @andreacappadona17 which I just saw now).

@jlooper This may render the persist workaround obsolete!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants