Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to update a document #45

Closed
24dev opened this issue Oct 21, 2018 · 4 comments
Closed

How to update a document #45

24dev opened this issue Oct 21, 2018 · 4 comments

Comments

@24dev
Copy link

24dev commented Oct 21, 2018

Loving the library so far, just having one issue!

After using .add(), the document appears correctly in the database. On .then, the docRef is also correct. However, using .set() on the document does not seem to work - the id is not being added to the document. I may be using it incorrectly, so have attached my code.

const restaurantsGeostoreRef = db.collection('exploreMap');
const restaurantsRef = new GeoFirestore(restaurantsGeostoreRef);
export const addNewRestaurant = data =>
  restaurantsRef
    .add({
      ...data,
      coordinates: new firebase.firestore.GeoPoint(data.coordinates[1], data.coordinates[0]),
    })
    .then(docRef =>
      restaurantsRef
        .set(docRef.id, {
          id: docRef.id,
        })
        .then(() => 'Successfully added.')
        .catch(error => error),
    )
    .catch(error => error);
@MichaelSolati
Copy link
Owner

The set function requires a full document, so the original with modifications, not just the modifications (as it looks for coordinates). So you'll want something like this:

const restaurantsGeostoreRef = db.collection('exploreMap');
const restaurantsRef = new GeoFirestore(restaurantsGeostoreRef);

export const addNewRestaurant = (data) => {
    const doc = {
        ...data,
        coordinates: new firebase.firestore.GeoPoint(data.coordinates[1], data.coordinates[0]),
    };

    restaurantsRef.add(doc)
        .then(docRef => restaurantsRef.set(docRef.id, { id: docRef.id, ...doc })
            .then(() => 'Successfully added.')
            .catch(error => error)
        ).catch(error => error);
}

@24dev
Copy link
Author

24dev commented Nov 8, 2018

Works, appreciate it!

@AndrewAi
Copy link

AndrewAi commented Nov 8, 2018

Can I still use firestore's set method, or would it be better to using geofirestores implementation of the set method? similarly with the remove method, if I just remove the document, it will just be deleted but all the other won't be affected.
Thank you for the library, its brilliant so far!

@MichaelSolati
Copy link
Owner

@AndrewAi I would advise you to use the libraries set method, as it has to parse your document for changes/adjustments. However for remove it wont matter.

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

No branches or pull requests

3 participants