I am trying to use geofirestore https://geofirestore.com/ with react native, the fact is that I have already implemented the methods to add elements in firestore, however I have read in the documentation that the data have to be added by geofirestore methods. Do I have to change all the methods and use the firestore methods? I use javascript and not typeScript, I see that there are examples with typescript and it seems that they have updated the library and other examples do not work. How could I add elements in the bbdd? I used geofirex and it was very simple but it has no limit, I want to change to geofirestore to be able to use this feature. Thank you!
2 Answers
so most if not all of the Typescript examples are almost identical to what you would do in JS (as well as the regular firebase library). Let me give you an example of adding and querying (with a limit) in react-native-firebase.
import firebase from 'react-native-firebase';
const doc = {
coordinates: new firebase.firestore.GeoPoint(0, 0),
name: 'The center of the WORLD'
};
const collection = firebase.firestore().collection('todos');
collection.add(doc).then((docRef) => {
collection.limit(100).get().then((querySnapshot) => {
// 100 items in query
querySnapshot.docChanges.forEach((change) => {
console.log(change.doc);
});
});
});
Now I've never used RNF, but from the docs I can gather that this is correct, and they do almost everything the same as the JS Firebase library (except docChanges returns as an array rather than a function that returns an array...). Anyway, lets see the same thing in Geofirestore with the added benefit of querying with limit and near a location!
import firebase from 'react-native-firebase';
import { GeoFirestore } from 'geofirestore';
const doc = {
coordinates: new firebase.firestore.GeoPoint(0, 0),
name: 'The center of the WORLD'
};
const geofirestore = new GeoFirestore(firebase.firestore());
const geocollection = geofirestore.collection('todos');
geocollection.add(doc).then((docRef) => {
geocollection.limit(100).near({
center: new firebase.firestore.GeoPoint(0, 0),
radius: 10
}).get().then((querySnapshot) => {
// 100 items in query within 10 KM of coordinates 0, 0
querySnapshot.docChanges().forEach((change) => {
console.log(change.doc);
});
});
});
Anyway, don't be afraid of the Typescript code samples, if you just strip the : GeoFirestore or whatever it's valid JS...
// This in TS
const firestore = firebase.firestore();
const geofirestore: GeoFirestore = new GeoFirestore(firestore);
// Is this in JS
const firestore = firebase.firestore();
const geofirestore = new GeoFirestore(firestore);
Finally I try to keep this viewers app up to date with Vanilla JS, if that helps.
This works perfectly
await eventRef.update({'d.arrUsers': firebase.firestore.FieldValue.arrayUnion(userUid)});
However I am unable to make this work, it is a map and I want to add another user, before it worked correctly...
await eventRef.set({'d.userMap': { [userUid]: { name: name, age: age } }}, {merge: true});
If I use update it deletes the one that exists and puts the new one, if I do so it gets it out of d in the document