0

My function is not returning anything when i want it to return an array based on the type argument. I tried to go with generic type but i couldn't assign my type Array to a t Array...

getNourritureFromDb(type){
   this.nourritureCollection = this.cantineappdb
   .collection("Inventaire")
   .doc("Nourriture")
   .collection(type);

 switch (type) {
   case "Plats": {
     this.nourritureCollection.ref.get().then(data => {

       let platArray : Plat[] = [];
       data.docs.forEach(doc => {
         let plat = new Plat("", 0, false, 0, "", "", [""], false);
         plat.name = doc.data().nourritureJson.name;
         plat.price = doc.data().nourritureJson.price;
         plat.ingredients = doc.data().nourritureJson.ingredients;
         plat.type = doc.data().nourritureJson.type;
         plat.availableQuantity = doc.data().nourritureJson.availableQuantity;
         plat.isAvailableOffMenu = doc.data().nourritureJson.isAvailableOffMenu;
         plat.imgUrl = doc.data().nourritureJson.imgUrl;
         plat.temp = doc.data().nourritureJson.temp;
         platArray.push(plat);
       });

         return platArray;

     });
     break;
   }
   case "Entrees": {
     this.nourritureCollection.ref.get().then(data => {
       let entreeArray : Entree[] = [];
       data.docs.forEach(doc => {
 ...
       });
       return entreeArray
     });
     break;
   }
   case "Snacks": {
     this.nourritureCollection.ref.get().then(data => {
       let snackArray : Snack[] = [];
       data.docs.forEach(doc => {
 ...
       return snackArray
     });
     break;
   }
   case "Boissons": {
     this.nourritureCollection.ref.get().then(data => {
       let boissonArray : Boisson[] = [];
       data.docs.forEach(doc => {
 ...
       return boissonArray
     });
     break;
   }
   case "Desserts": {
     this.nourritureCollection.ref.get().then(data => {
 ...
       return dessertArray
     });
     break;
   }
   default : {
     let emptyArray: any[] = [];
     console.log("Aucun type sélectionné");
     return emptyArray
   }
 }
}

I want my function to return the kind of array that was called in the type argument. My goal would be that i could call something like let plat[] = getNourritureFromDb("Plats");

Julien FEGER
  • 317
  • 5
  • 21
  • You need to return the promise or use [`async await`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function), but *do* read the linked dupe. – msanford Feb 01 '19 at 21:15
  • To expand on the above comment ... if each of these `this.nourritureCollection.ref.get()` is an async operation (gets data from a backend for example), then the call simply executes the request. The code within the `then` is not executed until some later point in time when the response is received. – DeborahK Feb 01 '19 at 21:15
  • You aren't returning the functions that hold those return statements. – Pytth Feb 01 '19 at 21:24
  • Thanks everyone. The dupe helped me and I did what you said, returning a promise containing my switch and it works perfectly. Async is really hard thanks again. – Julien FEGER Feb 01 '19 at 21:33

0 Answers0