0
trouve(id:number):boolean{  
    var bool :boolean =false
    this.comapanyService.query().subscribe((resula)=>{
           for (let i = 0; i < resula.json.length; i++) {
               try {
             if( id == resula.json[i].user.id)
                 {
   console.log('id-user:',id ,'user in company:',resula.json[i].user.id,'company :',resula.json[i].id )
                     bool = true
                   }
                } 
               catch (error) {}
           }
           console.log('bool fin pour :',bool)
      })
      console.log(bool)
      if(bool) return true;else return false
  }

 confirmDelete(login) {
    this.userService.find(login).subscribe((response)=>{
         const id =response.id
        console.log('resulta :', this.trouve(id))
    })
}
  • resulta : false
  • id-user: 7202
  • user in company: 7202 company : 5151
  • bool fin pour : true

Why do I always get a false?

Blauharley
  • 4,186
  • 6
  • 28
  • 47

1 Answers1

0

Because this.comapanyService.query() is an async operation and takes a little bit. That means the lines below will always get executed before the result comes in (when the bool value was not changed, yet):

var bool :boolean =false
console.log(bool)
if(bool) return true;else return false


*oh now we have an answer*
do other stuff inside the subscribe
hogan
  • 1,434
  • 1
  • 15
  • 32