Skip to content
This repository has been archived by the owner on Sep 7, 2022. It is now read-only.

[ Dartify ] Datasnapshot.val() failing #364

Closed
tperraut opened this issue Dec 1, 2020 · 4 comments
Closed

[ Dartify ] Datasnapshot.val() failing #364

tperraut opened this issue Dec 1, 2020 · 4 comments
Assignees

Comments

@tperraut
Copy link

tperraut commented Dec 1, 2020

I tried to use firebase database from firebase.dart package. Everything is working fine with primitive types but it seems the val() and toJson() functions on DataSnapshot are failing with exception :

Expected a value of type 'JSObject<Qp>', but got one of type 'NativeJavaScriptObject'

Calling numChildren() or all other function not depending on dartify helper return the expected values.

final complexObjectCollection = await database().ref("/rest/scooters").once("value");
final primitiveType = event.snapshot.child("S000893/online");
print(primitiveType.val()) // print true
print(complexObjectCollection.val()) // print nothing
print("Never shown") // this never print

The collection "/rest/scooters" have objects like the following inside :

{
  "adopted" : true,
  "batteryAlertSentTo" : "",
  "batteryLevel" : 0,
  "charging" : false,
  "checkedoutForOPS" : false,
  "currentSpeedMode" : "2",
  "defaultSpeedMode" : 2,
  "gender" : "female",
  "ghost" : false,
  "iotCode" : "XX",
  "iotType" : "XX",
  "lastTelemetryUpdate" : "2020-07-17T12:15:33Z",
  "locked" : true,
  "macAddress" : "XX",
  "name" : "Enable ⚡",
  "needsCollection" : false,
  "online" : true,
  "position" : {
    "latitude" : 44.83238,
    "longitude" : -0.53731
  },
  "reason" : "0",
  "region" : "Bordeaux",
  "status" : "HIDDEN",
  "vehicleCode" : "XX"
}

So something is happening in the dartify helper, it seems to be related to firestore types like GeoPoint and dart being unable to cast an object with latitude and longitude fields to it as it is actually not a firestore GeoPoint object.

Thank you for your work

@kevmoo
Copy link
Contributor

kevmoo commented Dec 16, 2020

I just tried to reproduce this in tests and had to problem

diff --git a/firebase/test/database_test.dart b/firebase/test/database_test.dart
index b7b382d..3db4051 100644
--- a/firebase/test/database_test.dart
+++ b/firebase/test/database_test.dart
@@ -60,6 +60,43 @@ void main() {
       key = null;
     });
 
+    test('complex', () async {
+      final myRef = database.ref('/rest/scooters');
+
+      final myKey = await myRef.push({
+        'adopted': true,
+        'batteryAlertSentTo': '',
+        'batteryLevel': 0,
+        'charging': false,
+        'checkedoutForOPS': false,
+        'currentSpeedMode': '2',
+        'defaultSpeedMode': 2,
+        'gender': 'female',
+        'ghost': false,
+        'iotCode': 'XX',
+        'iotType': 'XX',
+        'lastTelemetryUpdate': '2020-07-17T12:15:33Z',
+        'locked': true,
+        'macAddress': 'XX',
+        'name': 'Enable ⚡',
+        'needsCollection': false,
+        'online': true,
+        'position': {'latitude': 44.83238, 'longitude': -0.53731},
+        'reason': '0',
+        'region': 'Bordeaux',
+        'status': 'HIDDEN',
+        'vehicleCode': 'XX'
+      }).future;
+
+      print(myKey.key);
+
+      final complexObjectCollection =
+          await database.ref('/rest/scooters').once('value');
+      final snapshot = complexObjectCollection.snapshot;
+      print(snapshot.val());
+      print(snapshot.toJson());
+    });
+
     test('has toJson', () {
       var toJsonString = ref.toJson() as String;
       expect(toJsonString, startsWith(databaseUrl));

Let me know if you can come up with a failing test. I'd love to help fix this!

@kevmoo kevmoo added the More information needed Waiting for information from the person who files the issue label Dec 16, 2020
@tperraut
Copy link
Author

If I remove the lines :

  if (util.hasProperty(jsObject, 'latitude') &&
      util.hasProperty(jsObject, 'longitude') &&
      js.objectKeys(jsObject).length == 2) {
    // This is likely a GeoPoint – return it as-is
    return jsObject as GeoPoint;
  }

In the dartify helper, everything is working fine. I will come with a working test soon.

@no-response no-response bot removed the More information needed Waiting for information from the person who files the issue label Dec 16, 2020
@kevmoo
Copy link
Contributor

kevmoo commented Dec 16, 2020

Ooo! Maybe #379 fixes this, too!

@kevmoo kevmoo self-assigned this Dec 16, 2020
@kevmoo kevmoo closed this as completed in b1d345a Dec 16, 2020
@kevmoo
Copy link
Contributor

kevmoo commented Dec 16, 2020

@tperraut – please grab the latest here and verify things are fixed!

You need help doing that, let me know!

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

No branches or pull requests

2 participants