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

Commit a1cb801

Browse files
#604 Date saved to Firestore get retrieved as strings
#577 Firestore References can't be parsed
1 parent caeaa7d commit a1cb801

File tree

7 files changed

+81
-39
lines changed

7 files changed

+81
-39
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
### Fixes
1616
- [#601](https://github.com/EddyVerbruggen/nativescript-plugin-firebase/issues/601) Error using admob
17+
- [#604](https://github.com/EddyVerbruggen/nativescript-plugin-firebase/issues/604) Date saved to Firestore get retrieved as strings
1718

1819

1920
## 5.1.2 (2018, January 8)

demo-ng/app/item/items.component.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,9 @@ export class ItemsComponent implements OnInit {
133133
.then((querySnapshot: firestore.QuerySnapshot) => {
134134
querySnapshot.forEach(doc => {
135135
console.log(`${doc.id} => ${JSON.stringify(doc.data())}`);
136+
// since there's a reference stored here, we can use that to retrieve its data
137+
const docRef: firestore.DocumentReference = doc.data().ref2sf;
138+
docRef.get().then(res => console.log("docref.get: " + JSON.stringify(res.data())));
136139
});
137140
})
138141
.catch(err => console.log("Get failed, error" + err));

demo-ng/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
"angularfire2": "^5.0.0-rc.4",
2525
"firebase": "^4.6.2",
2626
"nativescript-angular": "~4.4.0",
27-
"nativescript-plugin-firebase": "5.1.2",
27+
"nativescript-plugin-firebase": "5.1.3",
2828
"nativescript-theme-core": "~1.0.2",
2929
"reflect-metadata": "~0.1.8",
3030
"rxjs": "~5.4.2",

demo/package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
{
22
"nativescript": {
33
"id": "org.nativescript.firebasedemo",
4-
"tns-android": {
4+
"tns-ios": {
55
"version": "3.4.0"
66
},
7-
"tns-ios": {
7+
"tns-android": {
88
"version": "3.4.0"
99
}
1010
},
1111
"dependencies": {
12-
"nativescript-plugin-firebase": "5.1.2",
12+
"nativescript-plugin-firebase": "5.1.3",
1313
"nativescript-theme-core": "^1.0.4",
1414
"nativescript-unit-test-runner": "^0.3.4",
1515
"tns-core-modules": "~3.4.0"

src/firebase.android.ts

Lines changed: 46 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ let fbCallbackManager = null;
2020
const GOOGLE_SIGNIN_INTENT_ID = 123;
2121
const REQUEST_INVITE_INTENT_ID = 48;
2222

23-
const gson = lazy(() => typeof(com.google.gson) === "undefined" ? null : new com.google.gson.Gson());
23+
// const gson = lazy(() => typeof(com.google.gson) === "undefined" ? null : new com.google.gson.Gson());
2424
const messagingEnabled = lazy(() => typeof(com.google.firebase.messaging) !== "undefined");
2525
const dynamicLinksEnabled = lazy(() => typeof(com.google.android.gms.appinvite) !== "undefined");
2626

@@ -167,13 +167,18 @@ firebase.toValue = val => {
167167
return returnVal;
168168
};
169169

170+
// no longer using Gson as fi Firestore's DocumentReference isn't serialized
170171
firebase.toJsObject = javaObj => {
171-
if (gson() !== null) {
172-
return JSON.parse(gson().toJson(javaObj));
173-
} else {
174-
// temp fallback for folks not having fetched gson yet in their build for some reason
175-
return firebase.toJsObjectLegacy(javaObj);
176-
}
172+
// if (gson() !== null) {
173+
// try {
174+
// return JSON.parse(gson().toJson(javaObj)); // this may fail if fi a DocumentReference is encountered
175+
// } catch (ignore) {
176+
// return firebase.toJsObjectLegacy(javaObj);
177+
// }
178+
// } else {
179+
// fallback for folks not having fetched gson yet in their build for some reason
180+
return firebase.toJsObjectLegacy(javaObj);
181+
// }
177182
};
178183

179184
firebase.toJsObjectLegacy = javaObj => {
@@ -191,18 +196,33 @@ firebase.toJsObjectLegacy = javaObj => {
191196
case 'java.lang.Long':
192197
case 'java.lang.Double':
193198
return Number(String(javaObj));
199+
case 'java.util.Date':
200+
return new Date(javaObj);
201+
case 'com.google.firebase.firestore.GeoPoint':
202+
return {
203+
"latitude": javaObj.getLatitude(),
204+
"longitude": javaObj.getLongitude()
205+
};
206+
case 'com.google.firebase.firestore.DocumentReference':
207+
const path: string = javaObj.getPath();
208+
const lastSlashIndex = path.lastIndexOf("/");
209+
return firebase.firestore._getDocumentReference(javaObj, path.substring(0, lastSlashIndex), path.substring(lastSlashIndex + 1));
194210
case 'java.util.ArrayList':
195211
node = [];
196212
for (let i = 0; i < javaObj.size(); i++) {
197213
node[i] = firebase.toJsObjectLegacy(javaObj.get(i));
198214
}
199215
break;
200216
default:
201-
node = {};
202-
const iterator = javaObj.entrySet().iterator();
203-
while (iterator.hasNext()) {
204-
const item = iterator.next();
205-
node[item.getKey()] = firebase.toJsObjectLegacy(item.getValue());
217+
try {
218+
node = {};
219+
const iterator = javaObj.entrySet().iterator();
220+
while (iterator.hasNext()) {
221+
const item = iterator.next();
222+
node[item.getKey()] = firebase.toJsObjectLegacy(item.getValue());
223+
}
224+
} catch (e) {
225+
console.log("PLEASE REPORT THIS AT https://github.com/NativeScript/NativeScript/issues: Tried to serialize an unsupported type: javaObj.getClass().getName(), error: " + e);
206226
}
207227
}
208228
return node;
@@ -2306,9 +2326,21 @@ firebase.firestore.onCollectionSnapshot = (colRef: com.google.firebase.firestore
23062326
return () => listener.remove();
23072327
};
23082328

2329+
firebase.firestore._getDocumentReference = (javaObj, collectionPath, documentPath): firestore.DocumentReference => {
2330+
return {
2331+
id: javaObj.getId(),
2332+
collection: cp => firebase.firestore.collection(`${collectionPath}/${documentPath}/${cp}`),
2333+
set: (data: any, options?: firestore.SetOptions) => firebase.firestore.set(collectionPath, javaObj.getId(), data, options),
2334+
get: () => firebase.firestore.getDocument(collectionPath, javaObj.getId()),
2335+
update: (data: any) => firebase.firestore.update(collectionPath, javaObj.getId(), data),
2336+
delete: () => firebase.firestore.delete(collectionPath, javaObj.getId()),
2337+
onSnapshot: (callback: (doc: DocumentSnapshot) => void) => firebase.firestore.onDocumentSnapshot(javaObj, callback),
2338+
android: javaObj
2339+
};
2340+
};
2341+
23092342
firebase.firestore.doc = (collectionPath: string, documentPath?: string): firestore.DocumentReference => {
23102343
try {
2311-
23122344
if (typeof(com.google.firebase.firestore) === "undefined") {
23132345
console.log("Make sure firebase-firestore is in the plugin's include.gradle");
23142346
return null;
@@ -2317,17 +2349,7 @@ firebase.firestore.doc = (collectionPath: string, documentPath?: string): firest
23172349
const db = com.google.firebase.firestore.FirebaseFirestore.getInstance();
23182350
const colRef: com.google.firebase.firestore.CollectionReference = db.collection(collectionPath);
23192351
const docRef: com.google.firebase.firestore.DocumentReference = documentPath ? colRef.document(documentPath) : colRef.document();
2320-
2321-
return {
2322-
id: docRef.getId(),
2323-
collection: cp => firebase.firestore.collection(`${collectionPath}/${documentPath}/${cp}`),
2324-
set: (data: any, options?: firestore.SetOptions) => firebase.firestore.set(collectionPath, docRef.getId(), data, options),
2325-
get: () => firebase.firestore.getDocument(collectionPath, docRef.getId()),
2326-
update: (data: any) => firebase.firestore.update(collectionPath, docRef.getId(), data),
2327-
delete: () => firebase.firestore.delete(collectionPath, docRef.getId()),
2328-
onSnapshot: (callback: (doc: DocumentSnapshot) => void) => firebase.firestore.onDocumentSnapshot(docRef, callback)
2329-
};
2330-
2352+
return firebase.firestore._getDocumentReference(docRef, collectionPath, documentPath);
23312353
} catch (ex) {
23322354
console.log("Error in firebase.firestore.doc: " + ex);
23332355
return null;

src/firebase.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -808,6 +808,8 @@ export namespace firestore {
808808
update: (document: any) => Promise<void>;
809809
delete: () => Promise<void>;
810810
onSnapshot(callback: (doc: DocumentSnapshot) => void): () => void;
811+
android?: any;
812+
ios?: any;
811813
}
812814

813815
export interface Query {

src/firebase.ios.ts

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -581,6 +581,17 @@ firebase.toJsObject = objCObj => {
581581
case 'Date':
582582
node[key] = new Date(val);
583583
break;
584+
case 'FIRDocumentReference':
585+
const path = (<FIRDocumentReference>val).path;
586+
const lastSlashIndex = path.lastIndexOf("/");
587+
node[key] = firebase.firestore._getDocumentReference(val, path.substring(0, lastSlashIndex), path.substring(lastSlashIndex + 1));
588+
break;
589+
case 'FIRGeoPoint':
590+
node[key] = {
591+
latitude: (<FIRGeoPoint>val).latitude,
592+
longitude: (<FIRGeoPoint>val).longitude
593+
};
594+
break;
584595
default:
585596
console.log("Please report this at https://github.com/EddyVerbruggen/nativescript-plugin-firebase/issues: iOS toJsObject is missing a converter for class '" + types.getClass(val) + "'. Casting to String as a fallback.");
586597
node[key] = String(val);
@@ -2285,6 +2296,19 @@ firebase.firestore.onCollectionSnapshot = (colRef: FIRCollectionReference, callb
22852296
}
22862297
};
22872298

2299+
firebase.firestore._getDocumentReference = (fIRDocumentReference, collectionPath, documentPath): firestore.DocumentReference => {
2300+
return {
2301+
id: fIRDocumentReference.documentID,
2302+
collection: cp => firebase.firestore.collection(`${collectionPath}/${documentPath}/${cp}`),
2303+
set: (data: any, options?: firestore.SetOptions) => firebase.firestore.set(collectionPath, fIRDocumentReference.documentID, data, options),
2304+
get: () => firebase.firestore.getDocument(collectionPath, fIRDocumentReference.documentID),
2305+
update: (data: any) => firebase.firestore.update(collectionPath, fIRDocumentReference.documentID, data),
2306+
delete: () => firebase.firestore.delete(collectionPath, fIRDocumentReference.documentID),
2307+
onSnapshot: (callback: (doc: DocumentSnapshot) => void) => firebase.firestore.onDocumentSnapshot(fIRDocumentReference, callback),
2308+
ios: fIRDocumentReference
2309+
};
2310+
};
2311+
22882312
firebase.firestore.doc = (collectionPath: string, documentPath?: string): firestore.DocumentReference => {
22892313
try {
22902314
if (typeof(FIRFirestore) === "undefined") {
@@ -2294,17 +2318,7 @@ firebase.firestore.doc = (collectionPath: string, documentPath?: string): firest
22942318

22952319
const fIRCollectionReference = FIRFirestore.firestore().collectionWithPath(collectionPath);
22962320
const fIRDocumentReference = documentPath ? fIRCollectionReference.documentWithPath(documentPath) : fIRCollectionReference.documentWithAutoID();
2297-
2298-
return {
2299-
id: fIRDocumentReference.documentID,
2300-
collection: cp => firebase.firestore.collection(`${collectionPath}/${documentPath}/${cp}`),
2301-
set: (data: any, options?: firestore.SetOptions) => firebase.firestore.set(collectionPath, fIRDocumentReference.documentID, data, options),
2302-
get: () => firebase.firestore.getDocument(collectionPath, fIRDocumentReference.documentID),
2303-
update: (data: any) => firebase.firestore.update(collectionPath, fIRDocumentReference.documentID, data),
2304-
delete: () => firebase.firestore.delete(collectionPath, fIRDocumentReference.documentID),
2305-
onSnapshot: (callback: (doc: DocumentSnapshot) => void) => firebase.firestore.onDocumentSnapshot(fIRDocumentReference, callback)
2306-
};
2307-
2321+
return firebase.firestore._getDocumentReference(fIRDocumentReference, collectionPath, documentPath);
23082322
} catch (ex) {
23092323
console.log("Error in firebase.firestore.doc: " + ex);
23102324
return null;

0 commit comments

Comments
 (0)