Add Geocode Attribut in Firestore Document

39 Views Asked by At

I have this code

const functions = require('firebase-functions');
const admin = require('firebase-admin');

admin.initializeApp();

const firestore = admin.firestore();


exports.processGzipCSVMonthly = functions.runWith({ timeoutSeconds: 540 }).https.onRequest(async (req, res) => {
    try {
      
        //...
        //data is content of a CSV file
        //...
        
        const collectionRef = firestore.collection('Adresses');
        let batch = firestore.batch();
    
        data.forEach((entry) => {
            const codeInsee = entry['code_insee'];
            const nomVoie = entry['nom_voie'];
            const numero = entry['numero'];
            const lon = parseFloat(entry['lon']); 
            const lat = parseFloat(entry['lat']); 
    
            // Génération d'un ID de document aléatoire
            const docId = firestore.collection('Temp').doc().id;
    
            // Créer un objet GeoPoint avec les valeurs de lat et lon
            const geoPoint = new admin.firestore.GeoPoint(lat, lon);
    
            // Enregistrement du de la rue et de ses adresse dans le document sous la collection Adresses
            batch.set(collectionRef.doc(docId), { numero, geoPoint, codeInsee, nom_voie: nomVoie });
        });

        await batch.commit();

        res.status(200).send('Traitement terminé avec succès.');
    } catch (error) {
        console.error('Erreur lors du traitement du fichier :', error);
        res.status(500).send('Une erreur est survenue lors du traitement.');
    }
});

On execution, I have an error when creating geoPoint variable with longitud ans latitude values

 const geoPoint = new admin.firestore.GeoPoint(lat, lon);

The error say that Geopoint is not a constructor.

TypeError: admin.firestore.GeoPoint is not a constructor

I use SDK 12.0 (latest). All doc say that this method is OK. I think there is a point that I did not see ...

Do you know why and can you help me ?

1

There are 1 best solutions below

0
Broshet On

Found it, Geopoint is part of google-cloud

const gcloud = require('@google-cloud/firestore');
const geoPoint = new gcloud.GeoPoint(lat, lon);

Thx for your help