How can I call a function with get data in Typescript?

Viewed 42

I can run a query in my main.ts file with another ts file, but I cannot pull the result of the query into another file. How can I do that?

My main.ts;

async function getAllTips() {
  const tips: any = []; // i need get "tips" array in another ts file
  try {
    const snapshot = await ref
      .child("tips")
      .orderByChild("cont")
      .startAt(1)
      .endAt(5)
      .get();
      
    if (snapshot.val()) {
      for (let key in snapshot.val()) {
        let value = snapshot.val()[key];
        tips.push(value.get("tips"));
      }
    }
  }  finally {
    return tips;
  }
}

async function asyncForArray(array,callback){
   for(let i=0;i<array.length;i++){
    await callback(array[i],i)
  }
  } 

async function asyncFor(obj,callback){
 for(let key in obj){
await callback(
  obj[key],key
)
}
} 

export {
  getAllTips
}

I need get "tips" array in another file. How can i do that? Best regards.

1 Answers

The only way to get a value from an internal variable of a function, is to return it, which you are already doing.

So you first export the function:

export async function getAllTips() {

Then in another file, you import the function:

import { getAllTips } from './tips'

And finally, call your function:

const tips = await getAllTips()
Related