How do i get my event to a variable type number?


#1

I emit an event and i want to receive the number that my event gives here’s my code:

here’s is where i emit my event, which is right

 editarProcesso(id){
    this.navCtrl.push(HomePage, {id : id})
    .then(() => {
      this.events.publish('id:added', id)
    });
    return console.log('id: ',id);
  }

Finally here’s where i get and aplly it:

this.model = new Processos();

        
        let idGenerator =  this.events.subscribe('id:added', (id) => {
          console.log('Generator' ,id)
        }) 
      

        

            if(idGenerator != null){
              this.processoProvider.getcamposProcesso(idGenerator)
              .then((result : any) =>{
                this.model = result;
                return console.log(idGenerator);
              }).catch((e) => console.log('Erro ao carregar os campos', e))
            }


      }

Problem log: [ts] Argument of type ‘void’ is not assignable to parameter of type ‘number’.
let idGenerator: void

The problem is the variable idGenerator receives a value null, and I and want to get value id, which is number???

Thanks in advance!


#2

I think you missunderstood the idea of Promise, Observer, Observable, async etc…

idGenerator is just a “listener”. the subscribe method is called when the listener received something, so this is where you should compare something.


#3

Sorry I m really dumb, i just need to do this:

this.events.subscribe('id:added', (id) => {
            if(id != null){
              this.processoProvider.getcamposProcesso(id)
              .then((result : any) =>{
                this.model = result;
                return console.log('id gerado:',id);
              }).catch((e) => console.log('Erro ao carregar os campos', e))
            }

        }); ```