Skip to content
Advertisement

Accessing Geopoints using Firebase Cloud Functions?

I have a collection called posts in Firestore which contains a documents with the fields content and location. Content is a string and Location is a Geopoint.

When accessing the database using Firebase Cloud Functions, I am able to print out the content by adding it into an array and JSON.stringfy the whole thing as a response.

  db.collection("posts").get().then((snapshot) => {
    snapshot.forEach((doc) => {

        var element = {
          "content": doc.data().content,
        }
        query = query.concat(element);
    });
    res.send(JSON.stringify(query));

However, If i try to emulate this with the geopoints, I simply get an empty response {}.

  db.collection("posts").get().then((snapshot) => {
    snapshot.forEach((doc) => {
        var latitude = doc.data().location.getLatitude();
        var longitude = doc.data().location.getLongitude();

        var element = {
          "latitude": latitude,
          "longitude": longitude,
        }
        query = query.concat(element);
    });
    res.send(JSON.stringify(query));

How do access the Geopoint values in the Firestore using Javascript so I can perform operations on/print them? i.e. find distance between two points.

Advertisement

Answer

I managed to solve this by accessing the latitude/longitude fields directly using doc.data().location.latitude.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement