1
votes

I'm new to both programming and Flutter/Firebase but I'm starting to get my head around it. I am trying to put together an app that combines user and location information via Firebase so that I can show multiple user locations on a map. What I need help with and would like to do is write the Firebase UID and user location to the same Firestore document via a Typescript cloud function.

I've been working with this Firebase background location plugin for several months and have figured out a lot about how it works. Concurrently I've been trying multiple Firebase Auth templates and have learned a fair amount about what API options are available. Most importantly I understand that the UID is the key value to identify my users.

I've also learned a fair amount about Firebase cloud functions and how to write specific data to a Firestore collection. I've learned how to write the user information and UID to a collection with a Javascript cloud function and I've also learned how to write the location information (lat, lng, etc...) to a collection via a Typescript cloud function.

My working Typescript is below, it has the location data I need. Can I modify the Typescript to include information such as UID or perhaps email address sourced from user data stored in Firebase? Thanks for reading and for any suggestions on how to proceed. Nathan

import * as functions from 'firebase-functions';

exports.createLocation = functions.firestore
  .document('locations/{locationId}')
  .onCreate((snap, context) => {
    const record = snap.data();

    const location = record.location;

    console.log('[data] - ', record);

    return snap.ref.set({
      uuid: location.uuid,
      timestamp: location.timestamp,
      is_moving: location.is_moving,
      latitude: location.coords.latitude,
      longitude: location.coords.longitude,
      speed: location.coords.speed,
      heading: location.coords.heading,
      altitude: location.coords.altitude,
      event: location.event,
      battery_is_charging: location.battery.is_charging,
      battery_level: location.battery.level,
      activity_type: location.activity.type,
      activity_confidence: location.activity.confidence,
      extras: location.extras,
    });
});
1
I'm unclear on what you are trying to implement and where you are stuck with that. You have a lot of background information here, but not so much of a description of a problem to solve.Doug Stevenson
How do I add the UID to the Typescript I have posted? I have a firestore collection named locations and the Typescript I have above posts everything to that folder through a cloud function, is it possible to add the firebase user (UID) to this typescript so the cloud function writes the current UID with the other values. Hope this clears things up.giroprotagonist

1 Answers

0
votes

What you're trying to do isn't possible in this scenario. In Firestore triggers, the user ID is not available. These triggers actually don't have any sense of user at all. If you have an ID to write, that should be added to the document as it's written on the client app.