MichaelSolati / geofirestore-js

Location-based querying and filtering using Firebase Firestore.
https://geofirestore.com
MIT License
504 stars 58 forks source link

Invalid key error when calling `add()` #98

Closed srobertson421 closed 5 years ago

srobertson421 commented 5 years ago

I'm trying to utilize the add() method. I followed the data structure listed here: https://github.com/geofirestore/spittin-hot-geofirestore/blob/6a9c3482f8437f594c78743eb4fd96aee777a63b/data/src/utils.ts#L19

I'm getting an error that the data has an invalid key. I was under the assumption the library looked for the coordinates key within the d object. Am I missing something or do I have to provide a key?

geofirestore_error

Here is my code:

My firebase config:

firebase.initializeApp(config);

firebase.firestore().enablePersistence()
.catch(function(err) {
  if (err.code === 'failed-precondition') {
    // Multiple tabs open, persistence can only be enabled
    // in one tab at a a time.
    // ...
  } else if (err.code === 'unimplemented') {
    // The current browser does not support all of the
    // features required to enable persistence
    // ...
  }
});

export const db = new GeoFirestore(firebase.firestore());
export const auth = firebase.auth();

Setting the state in my React component, I'm building the geodata object out manually:

const place = {
        address: newAddress,
        coordinates: new firebase.firestore.GeoPoint(latLng.lat, latLng.lng),
        owner: auth.currentUser.uid,
        submittedBy: auth.currentUser.email,
        description: placeDescription,
      }

      setNewPlace({
        g: Geokit.hash({ lat: place.coordinates.latitude, lng: place.coordinates.longitude }),
        l: place.coordinates,
        d: place,
      });

Then calling the add method:

db.collection('places').add(newPlace);
srobertson421 commented 5 years ago

Fiddled around and used the doc().set() method instead. Works just fine so we're good there, but still curious why add() was failing.

MichaelSolati commented 5 years ago

So add doesn't require the object details you're putting in with your setNewPlace function. It just takes the place object and creates all of those fields. I'm like 99.99% confident thats the problem. Don't build the object manually and just send the place object.

srobertson421 commented 5 years ago

Ah got it, ok that makes sense. Thanks for the quick response!