My FeedDiscussionsHeadless CMS
New
Sign in
Log inSign up
Learn more about Hashnode Headless CMSHashnode Headless CMS
Collaborate seamlessly with Hashnode Headless CMS for Enterprise.
Upgrade ✨Learn more

How do I access a class property within a nested function?

Dandy Cheng's photo
Dandy Cheng
·Jul 7, 2019

Consider the following code:

class Person{
     constructor(name){
          this.name = name
     }
     findFromDatabase(name){
          return new Promise(function(resolve,reject)){
               this.userData = DatabaseMethod(name) // This binds userData to the promise 
          })
     }
}

I intend to bind userData to the Person class, but using this.userData = ... Binds it to the promise. How can I achieve what I intended to do? Thanks.