Using TypeScript cloud functions to send Firebase UID to a document output

I'm fairly new to programming as well as Flutter/Firebase, but I'm making progress in understanding it. My goal is to create an app that integrates user and location data using Firebase, allowing me to display multiple user locations on a map. What I really need assistance with is figuring out how to write the Firebase UID and user location information within the same Firestore document using a Typescript cloud function.

Over the past few months, I've been experimenting with this Firebase background location plugin and have gained significant insights into its functionality. In parallel, I've been exploring various Firebase Auth templates to understand the available API options. The crucial realization for me has been the importance of the UID as the primary identifier for my users.

Additionally, I've delved into Firebase cloud functions and learned how to efficiently write specific data to a Firestore collection. Using a Javascript cloud function, I managed to store user information along with their UID in a collection, while a Typescript cloud function allowed me to save location details (latitude, longitude, etc.) successfully.

Below is the Typescript code snippet that contains the essential location data. I'm now wondering if there's a way to modify this script to also include additional details like the UID or email address obtained from the user data stored in Firebase. Any suggestions on advancing further would be highly appreciated. Thank you for taking the time to read this. 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,
    });
});

Answer №1

Unfortunately, achieving your goal is not feasible given the circumstances. When it comes to Firestore triggers, the user's ID remains unavailable. These triggers lack any knowledge of a specific user. If there is an ID that needs to be included, it should be appended to the document during the client app's writing process.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Generate md-card components in real-time using data fetched from an API

I have a scenario where I make an API call to fetch user profiles, and I want to generate Angular Material Design md-cards dynamically for each profile. The number of profiles retrieved can vary, hence the need for dynamic card creation. Below is the comp ...

Combine multiple objects to create a new object that represents the intersection of all properties

Imagine you have these three objects: const obj = { name: 'bob', }; const obj2 = { foo: 'bar', }; const obj3 = { fizz: 'buzz', }; A simple merge function has been written to combine these three objects into one: ...

How can I obtain the download link for an image that has been resized using Node.js in Google Cloud Functions?

I have recently started exploring node js and google cloud functions. Successfully, I can resize an image to create a thumbnail. However, I am stuck at finding the download URL for the newly generated thumbnail. Below is the code snippet: exports.gener ...

Tips for converting string values from an Observable to numbers using the parseFloat() method

Having trouble converting text to numbers for geolocation coordinates. My model consists of a site with an ID and an array of points as a property. Rather than creating a relationship between site and points, I've structured it differently. In my cod ...

Understanding how to infer the type of a function when it is passed as an argument

Looking at the images below, I am facing an issue with my function that accepts options in the form of an object where one of the arguments is a transform function. The problem is that while the type of the response argument is correctly inferred for the e ...

Alert of incompatible browser in application developed using create-react-app

After developing a web application using create-react-app, I encountered the issue of unsupported browsers such as IE10. Users with older browsers need to be notified to switch to a more up-to-date one for optimal performance. I find myself in a predicame ...

What is the process of invoking a function in Typescript?

I am curious about how to effectively call this function in TypeScript. Can you guide me on the correct way to do it? type Fish = { swim: () => void }; type Bird = { fly: () => void }; function move(animal: Fish | Bird) { if ("swim" in ...

The proxy is unable to access the method within the subclass

Issues are arising as I attempt to utilize a Proxy. The structure of my class is as follows: export class Builder { public doSomething(...args: (string | number | Raw | Object)[]): this { // Do stuff return this } } export class M ...

The data type 'DocumentData' cannot be assigned to type 'IProduct'

I am new to using TypeScript and I could really use some help. I'm stuck on how to fix this error. Interfaces export interface IProduct { name: string; price: number[]; stock: number; createdAt: firestore.Timestamp } export interface ID ...

Utilizing an external library in a Typescript 2.0 project: A comprehensive guide

I am currently working on a Typescript 2.0 project that utilizes Common JS modules and System JS loader. My IDE of choice is Visual Studio code. I am encountering a challenge with integrating an external library (filesaver JS) into my project. After insta ...

Script execution is disabled on this system preventing the loading of content - ANGULAR V14

Every time I try to run my Angular project or any ng command, I keep encountering the following error message: ng : File C:\Users\achra\AppData\Roaming\npm\ng.ps1 cannot be loaded because running scripts is disabled on this ...

The Angular Nativescript framework is lacking necessary properties for the 'Observable<>' type

Whenever I try to set my function as getCalendarEvents(): Observable<Array<CalendarEvent>>{, I encounter an error message stating Type 'Observable<CalendarEvent[]>' is missing the following properties from type 'CalendarEve ...

What is the best method to retrieve the nested value in this JavaScript/HTML code?

Can anyone help me extract the value "Yes, I am a Speaker" from this code using Javascript DOM element with getElementById? The challenge is that the value is nested inside a list element without any specific attributes. Each block of code has a unique l ...

Error occurred during the deployment of a Firebase cloud function

Encountering a Firebase deployment error when attempting to run both code listening on a port and a cloud function in a Node.js server file The issue: Cloud Run error: Container failed to start. Failed to start and then listen on the port defined by the ...

Having trouble setting default value using formControl in a component shared across multiple forms

Currently, I am facing an issue with setting a default value in a custom dropdown by using formControlName. In order to attach formControls to a shared component, I am utilizing ControlValueAccessors in the shared component. This allows me to update the fo ...

Combining Multiple TypeScript Declaration Files into an NPM Package for Seamless Importing with index.d.ts

I have a unique package structure that includes: myPackage/ |- types/ | |- type1.d.ts | |- type2.d.ts |- src/ | |- someUtilities.ts |- index.ts |- package.json |- tsconfig.json In my package, the index.ts file is handling imports and exports for all th ...

The iPhone 11 is unable to run any of the runner's architectures (armv7, arm64) as it operates on Intel 64-bit architecture

After attempting to run my flutter(2.2.x) project in XCode Version 12.5 (12E262) today, an error message popped up: Runner's architectures (armv7, arm64) include none that iPhone 11 can execute (Intel 64-bit). Here is a glimpse of the UI: https://i. ...

Call a function within another function of the same class in Angular2, and utilize the properties of the first function in the

I am working on a class that contains two functions, both of which retrieve data from API calls. Let's start with the First function getQuotes(){ this.quoteService.getQuotes() .subscribe( quotes => { this.quotCode = this.quo ...

Removing a header in angular based on a specific name - a step-by-step guide!

Within my application, I am making multiple HTTP requests that all require an authentication header with an access token (along with other header values). I have defined a header variable of type HttpHeaders (refer to code#1) to be used as follows: this.ht ...

Can the output type of a function be dynamically determined by using the function parameter name as the property in an interface?

I am attempting to dynamically assign the output of a function based on the name of the input parameter within an existing interface: interface MyInterface { typeA: string; typeB: boolean; typeC: string; typeD: number; ... } const myFunction: ( ...