Can you provide guidance on how to extract the ID from a Firebase reference in the "onCreate" or "onUpdate" triggers?

I have a scenario where I need to fetch specific values under {id} in the .onUpdate() method. Var3 is a nested object, while var2 is a single variable. Is there a way to extract {id} from onUpdate and pass it as an argument to customMethod so that I can utilize it in admin.ref()?

Code snippet from index.ts:

export const funcName = functions.region("someRegion").database.ref('/user/{id}/var3').onUpdate((change, context) =>{

      const originalData = <varType> change.after.val();
      const processedData = customMethod(originalData);

      if(change.after.ref.parent)
      return change.after.ref.parent.child('custom').set(processedData);
      else
      return null;
});


function customMethod(data: varType){
 return admin.database().ref('/user/{id}/var2').once('value').then(function(snapshot){
// processing stuff after storing value from var2 in a local variable
}

Answer №1

Finally got it to work by accessing the parent node and passing the reference as a parameter.

 const originalData = <varType> change.after.val();
 const splRef = <admin.database.Reference> change.after.ref.parent
 const processedData = customMethod(originalData,splRef);
function customMethod(data: varType,dbref: admin.database.Reference){
 return admin.database().dbref.once('value').then(function(snapshot){
// some processing happens here after storing value from var2 in a local variable
}

Shout out to @Renaud for the support!

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

From jQuery to Angular using Typescript

I have been trying to convert the following code snippets into Typescript, but haven't found a solution yet. Can you please advise me on how to achieve this functionality? $('html').addClass('abc'); $(".abc .wrap").css("height", h ...

Tips for creating a type-safe union typed save method

My goal is to build a versatile http service that includes a method like save(value), which in turn triggers either create(value) or update(value). What sets this requirement apart is the optional configuration feature where the type of value accepted by c ...

Creating a TypeScript rule/config to trigger an error when a React Functional Component does not return JSX

I've encountered a recurring issue where I forget to include a return statement when rendering JSX in a functional component. Here's an example of the mistake: const Greetings = function Greetings({name}) { <div>Greetings {name}</div& ...

Error in TypeScript: Angular Jasmine - The argument given as type 'string' cannot be assigned to a parameter expecting type 'never'

Currently, I am in the process of writing test cases for Angular using Jasmine 3.6.0 and TypeScript 4.1.5 with "strict": false set in my tsconfig.json file. One particular task involves spying on a component method called 'close', and following ...

The attribute specified is not present on the element within the array

I'm attempting to create an array that includes an object with initialized properties and a number. Yet, I encounter this error message: The error states: 'Property 'foo' does not exist on type 'number | IObj'. The proper ...

How to anticipate an error being thrown by an observable in RxJS

Within my TypeScript application, there exists a method that produces an rxjs Observable. Under certain conditions, this method may use the throwError function: import { throwError } from 'rxjs'; // ... getSomeData(inputValue): Observable<s ...

The type 'Function' does not contain any construct signatures.ts

Struggling to transition my JS code to TS, specifically with a class called Point2D for handling 2 dimensional points. Encountering an error message stating Type 'Function' has no construct signatures.ts(2351). Any insights on what might be going ...

Eliminating Firebase data from search by its unique identifier

Here's something I've been pondering. I have a lookup location in my database where I track whether a username has already been taken. The data is stored in the format username:userid. Everything was fine until I realized I needed to keep my data ...

How to use TypeScript to modify button styling with an OnClick() event handler

Learning TypeScript with Existing Code Transition Currently, I am delving into the world of TypeScript and in the process of converting my CoffeeScript code to TypeScript (specifically Lit Web Component). Confusion on Translation Process I'm encount ...

Node 19820 threw an uncaught promise rejection warning due to a TypeError stating that it cannot read the property 'byteLength' of an undefined value

I'm currently working on developing an API with the use of Express to enable the upload of images to Firebase storage. However, whenever I try to access this particular endpoint, an error message is displayed: "(node:19820) UnhandledPromiseRejectionWa ...

The issue lies in the error code TS2315 which states that the type 'observable' is not a generic

I keep encountering an error message that says 'observable is not generic' while importing files. I am working on implementing CRUD operations in Angular 7, where I have created two components for adding and listing employees. The functions for c ...

The design system package may experience difficulty loading material styles correctly

Our company is currently in the process of developing a design system that can be easily integrated into multiple projects as a package. While building the package has been successful, we encounter an error after installing it and trying to import the them ...

Tips for accessing and manipulating an array that is defined within a Pinia store

I have set up a store to utilize the User resource, which includes an array of roles. My goal is to search for a specific role within this array. I've attempted to use Array functions, but they are not compatible with PropType<T[]>. import route ...

Unexpected error encountered in Angular 2 beta: IE 10 displays 'Potentially unhandled rejection [3] SyntaxError: Expected'

Question regarding Angular 2 Beta: I am starting off with a general overview in the hopes that this issue is already recognized, and I simply overlooked something during my research. Initially, when Angular 2 Beta.0 was released, I managed to run a basic m ...

Modify the content of a separate division by selecting a different item in a list with the help of Vue.js and TypeScript

I am still learning Vue and may not have all the answers. Currently, I am working on a feature that changes the text of another div based on the item I select from a list. You can find the code sandbox link below along with my current implementation. Code ...

Incompatible TypeScript function declarations

Here's an example code snippet that is causing me some confusion due to an error. In the setFilter method, I have specified the type of T as Product. Therefore, I would expect to be able to successfully set the filter, since both T and Product share ...

Is there a way to differentiate between the AzDO extension running in a YAML pipeline versus a Classic pipeline?

I've searched extensively, but haven't come across a solution yet. Does anyone have a method to determine if your extension is operating in Classic or YAML pipeline? I'm currently developing an extension that generates ANSI-colored output, ...

I'm searching for TypeScript interfaces that can be used to define OpenAPI Json. Where can I

If you're looking to implement the OpenApi specifications for your project, there are a variety of fields and values that need to be set. For a detailed look at these specifications, you can refer to the documentation here. In an effort to streamline ...

Solving TypeScript Error in React for State Destructuring

Recently, I've been working on creating a React component for AutoComplete based on a tutorial. In my development process, I am using Typescript. However, I encountered an issue while trying to destructure the state within the render suggestions metho ...

Utilizing Typescript DOM library for server-side operations

I've been working on coding a Google Cloud Function that involves using the URL standard along with URLSearchParams. After discovering that they are included in the TypeScript DOM library, I made sure to add it to my tsconfig file under the lib settin ...