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

Commit

Permalink
#604 Date saved to Firestore get retrieved as strings
Browse files Browse the repository at this point in the history
#577 Firestore References can't be parsed
  • Loading branch information
EddyVerbruggen committed Jan 15, 2018
1 parent caeaa7d commit a1cb801
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 39 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

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


## 5.1.2 (2018, January 8)
Expand Down
3 changes: 3 additions & 0 deletions demo-ng/app/item/items.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,9 @@ export class ItemsComponent implements OnInit {
.then((querySnapshot: firestore.QuerySnapshot) => {
querySnapshot.forEach(doc => {
console.log(`${doc.id} => ${JSON.stringify(doc.data())}`);
// since there's a reference stored here, we can use that to retrieve its data
const docRef: firestore.DocumentReference = doc.data().ref2sf;
docRef.get().then(res => console.log("docref.get: " + JSON.stringify(res.data())));
});
})
.catch(err => console.log("Get failed, error" + err));
Expand Down
2 changes: 1 addition & 1 deletion demo-ng/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"angularfire2": "^5.0.0-rc.4",
"firebase": "^4.6.2",
"nativescript-angular": "~4.4.0",
"nativescript-plugin-firebase": "5.1.2",
"nativescript-plugin-firebase": "5.1.3",
"nativescript-theme-core": "~1.0.2",
"reflect-metadata": "~0.1.8",
"rxjs": "~5.4.2",
Expand Down
6 changes: 3 additions & 3 deletions demo/package.json
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
{
"nativescript": {
"id": "org.nativescript.firebasedemo",
"tns-android": {
"tns-ios": {
"version": "3.4.0"
},
"tns-ios": {
"tns-android": {
"version": "3.4.0"
}
},
"dependencies": {
"nativescript-plugin-firebase": "5.1.2",
"nativescript-plugin-firebase": "5.1.3",
"nativescript-theme-core": "^1.0.4",
"nativescript-unit-test-runner": "^0.3.4",
"tns-core-modules": "~3.4.0"
Expand Down
70 changes: 46 additions & 24 deletions src/firebase.android.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ let fbCallbackManager = null;
const GOOGLE_SIGNIN_INTENT_ID = 123;
const REQUEST_INVITE_INTENT_ID = 48;

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

Expand Down Expand Up @@ -167,13 +167,18 @@ firebase.toValue = val => {
return returnVal;
};

// no longer using Gson as fi Firestore's DocumentReference isn't serialized
firebase.toJsObject = javaObj => {
if (gson() !== null) {
return JSON.parse(gson().toJson(javaObj));
} else {
// temp fallback for folks not having fetched gson yet in their build for some reason
return firebase.toJsObjectLegacy(javaObj);
}
// if (gson() !== null) {
// try {
// return JSON.parse(gson().toJson(javaObj)); // this may fail if fi a DocumentReference is encountered
// } catch (ignore) {
// return firebase.toJsObjectLegacy(javaObj);
// }
// } else {
// fallback for folks not having fetched gson yet in their build for some reason
return firebase.toJsObjectLegacy(javaObj);
// }
};

firebase.toJsObjectLegacy = javaObj => {
Expand All @@ -191,18 +196,33 @@ firebase.toJsObjectLegacy = javaObj => {
case 'java.lang.Long':
case 'java.lang.Double':
return Number(String(javaObj));
case 'java.util.Date':
return new Date(javaObj);
case 'com.google.firebase.firestore.GeoPoint':
return {
"latitude": javaObj.getLatitude(),
"longitude": javaObj.getLongitude()
};
case 'com.google.firebase.firestore.DocumentReference':
const path: string = javaObj.getPath();
const lastSlashIndex = path.lastIndexOf("/");
return firebase.firestore._getDocumentReference(javaObj, path.substring(0, lastSlashIndex), path.substring(lastSlashIndex + 1));
case 'java.util.ArrayList':
node = [];
for (let i = 0; i < javaObj.size(); i++) {
node[i] = firebase.toJsObjectLegacy(javaObj.get(i));
}
break;
default:
node = {};
const iterator = javaObj.entrySet().iterator();
while (iterator.hasNext()) {
const item = iterator.next();
node[item.getKey()] = firebase.toJsObjectLegacy(item.getValue());
try {
node = {};
const iterator = javaObj.entrySet().iterator();
while (iterator.hasNext()) {
const item = iterator.next();
node[item.getKey()] = firebase.toJsObjectLegacy(item.getValue());
}
} catch (e) {
console.log("PLEASE REPORT THIS AT https://github.com/NativeScript/NativeScript/issues: Tried to serialize an unsupported type: javaObj.getClass().getName(), error: " + e);
}
}
return node;
Expand Down Expand Up @@ -2306,9 +2326,21 @@ firebase.firestore.onCollectionSnapshot = (colRef: com.google.firebase.firestore
return () => listener.remove();
};

firebase.firestore._getDocumentReference = (javaObj, collectionPath, documentPath): firestore.DocumentReference => {
return {
id: javaObj.getId(),
collection: cp => firebase.firestore.collection(`${collectionPath}/${documentPath}/${cp}`),
set: (data: any, options?: firestore.SetOptions) => firebase.firestore.set(collectionPath, javaObj.getId(), data, options),
get: () => firebase.firestore.getDocument(collectionPath, javaObj.getId()),
update: (data: any) => firebase.firestore.update(collectionPath, javaObj.getId(), data),
delete: () => firebase.firestore.delete(collectionPath, javaObj.getId()),
onSnapshot: (callback: (doc: DocumentSnapshot) => void) => firebase.firestore.onDocumentSnapshot(javaObj, callback),
android: javaObj
};
};

firebase.firestore.doc = (collectionPath: string, documentPath?: string): firestore.DocumentReference => {
try {

if (typeof(com.google.firebase.firestore) === "undefined") {
console.log("Make sure firebase-firestore is in the plugin's include.gradle");
return null;
Expand All @@ -2317,17 +2349,7 @@ firebase.firestore.doc = (collectionPath: string, documentPath?: string): firest
const db = com.google.firebase.firestore.FirebaseFirestore.getInstance();
const colRef: com.google.firebase.firestore.CollectionReference = db.collection(collectionPath);
const docRef: com.google.firebase.firestore.DocumentReference = documentPath ? colRef.document(documentPath) : colRef.document();

return {
id: docRef.getId(),
collection: cp => firebase.firestore.collection(`${collectionPath}/${documentPath}/${cp}`),
set: (data: any, options?: firestore.SetOptions) => firebase.firestore.set(collectionPath, docRef.getId(), data, options),
get: () => firebase.firestore.getDocument(collectionPath, docRef.getId()),
update: (data: any) => firebase.firestore.update(collectionPath, docRef.getId(), data),
delete: () => firebase.firestore.delete(collectionPath, docRef.getId()),
onSnapshot: (callback: (doc: DocumentSnapshot) => void) => firebase.firestore.onDocumentSnapshot(docRef, callback)
};

return firebase.firestore._getDocumentReference(docRef, collectionPath, documentPath);
} catch (ex) {
console.log("Error in firebase.firestore.doc: " + ex);
return null;
Expand Down
2 changes: 2 additions & 0 deletions src/firebase.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -808,6 +808,8 @@ export namespace firestore {
update: (document: any) => Promise<void>;
delete: () => Promise<void>;
onSnapshot(callback: (doc: DocumentSnapshot) => void): () => void;
android?: any;
ios?: any;
}

export interface Query {
Expand Down
36 changes: 25 additions & 11 deletions src/firebase.ios.ts
Original file line number Diff line number Diff line change
Expand Up @@ -581,6 +581,17 @@ firebase.toJsObject = objCObj => {
case 'Date':
node[key] = new Date(val);
break;
case 'FIRDocumentReference':
const path = (<FIRDocumentReference>val).path;
const lastSlashIndex = path.lastIndexOf("/");
node[key] = firebase.firestore._getDocumentReference(val, path.substring(0, lastSlashIndex), path.substring(lastSlashIndex + 1));
break;
case 'FIRGeoPoint':
node[key] = {
latitude: (<FIRGeoPoint>val).latitude,
longitude: (<FIRGeoPoint>val).longitude
};
break;
default:
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.");
node[key] = String(val);
Expand Down Expand Up @@ -2285,6 +2296,19 @@ firebase.firestore.onCollectionSnapshot = (colRef: FIRCollectionReference, callb
}
};

firebase.firestore._getDocumentReference = (fIRDocumentReference, collectionPath, documentPath): firestore.DocumentReference => {
return {
id: fIRDocumentReference.documentID,
collection: cp => firebase.firestore.collection(`${collectionPath}/${documentPath}/${cp}`),
set: (data: any, options?: firestore.SetOptions) => firebase.firestore.set(collectionPath, fIRDocumentReference.documentID, data, options),
get: () => firebase.firestore.getDocument(collectionPath, fIRDocumentReference.documentID),
update: (data: any) => firebase.firestore.update(collectionPath, fIRDocumentReference.documentID, data),
delete: () => firebase.firestore.delete(collectionPath, fIRDocumentReference.documentID),
onSnapshot: (callback: (doc: DocumentSnapshot) => void) => firebase.firestore.onDocumentSnapshot(fIRDocumentReference, callback),
ios: fIRDocumentReference
};
};

firebase.firestore.doc = (collectionPath: string, documentPath?: string): firestore.DocumentReference => {
try {
if (typeof(FIRFirestore) === "undefined") {
Expand All @@ -2294,17 +2318,7 @@ firebase.firestore.doc = (collectionPath: string, documentPath?: string): firest

const fIRCollectionReference = FIRFirestore.firestore().collectionWithPath(collectionPath);
const fIRDocumentReference = documentPath ? fIRCollectionReference.documentWithPath(documentPath) : fIRCollectionReference.documentWithAutoID();

return {
id: fIRDocumentReference.documentID,
collection: cp => firebase.firestore.collection(`${collectionPath}/${documentPath}/${cp}`),
set: (data: any, options?: firestore.SetOptions) => firebase.firestore.set(collectionPath, fIRDocumentReference.documentID, data, options),
get: () => firebase.firestore.getDocument(collectionPath, fIRDocumentReference.documentID),
update: (data: any) => firebase.firestore.update(collectionPath, fIRDocumentReference.documentID, data),
delete: () => firebase.firestore.delete(collectionPath, fIRDocumentReference.documentID),
onSnapshot: (callback: (doc: DocumentSnapshot) => void) => firebase.firestore.onDocumentSnapshot(fIRDocumentReference, callback)
};

return firebase.firestore._getDocumentReference(fIRDocumentReference, collectionPath, documentPath);
} catch (ex) {
console.log("Error in firebase.firestore.doc: " + ex);
return null;
Expand Down

0 comments on commit a1cb801

Please sign in to comment.