Run only once the valueChanges () function within a promise

0

I have the following function that returns the data stored in cloud firestore,

verifica_en_firebase(nombre: string) {

return new Promise((resolve, reject) => {
  this.doc = this.db.doc('/usuarios/${nombre}')
      .valueChanges().subscribe(data => {

          if (data) {
              //console.log("ok hay datos");

              console.log("datos--> " + JSON.stringify(data['markes']));

              for (var i = 0; i < data['markes'].length; i++) {
                console.log('obj.' + i + " ->" + data['markes'][i].latitud);

                this.data_marker.push({
                  latitud: data['markes'][i].latitud,
                  longitud: data['markes'][i].longitud
                });
              }

          } else {

          }
      });
  });

but is it possible that this function is executed only once? , researching a bit I found this function take (1) and I use it in the following way:

return new Promise((resolve, reject) => {
  this.doc = this.db.doc('/usuarios/${nombre}')
      .valueChanges().take(1).subscribe(data => {

          if (data) {
              //console.log("ok hay datos");

              console.log("datos--> " + JSON.stringify(data['markes']));

              for (var i = 0; i < data['markes'].length; i++) {
                console.log('obj.' + i + " ->" + data['markes'][i].latitud);

                this.data_marker.push({
                  latitud: data['markes'][i].latitud,
                  longitud: data['markes'][i].longitud
                });
              }
              console.log("datos-->2 " + JSON.stringify( this.data_marker));

          } else {

          }
      });
   });

but it does not recognize it, and it sends me error there will be another way to make this function run only once?

    
asked by Eze 02.08.2018 в 19:27
source

0 answers