Access an object from outside a promise in ionic

I’m a newbie in programming and this is my first time asking a question on Stackoverflow too.
so I’m working on a project and I want to access "Totalcreditscore1" from outside the promise.
I’ve been getting undefined.
I am able to return it on console log without issues but outside the promise it is difficult.
I’ve seen a lot of similar problems on StackOverflow but I don’t seem to know how to fix the problem.
I’ve been on this for like two days now.
please help.
thanks in advance.

here is my code

     ngOnInit() {
// //fetch the value of Totalcreditworthyscore from firestore

var docRef = this.db.collection("Bio").doc("test")
    docRef.get().toPromise().then(async (doc) => {
      if (doc.exists) {
            this.docData=this.data
            this.Totalcreditworthyscore1=(this.data.totalcreditscore*1)
             console.log("Kayodes Document data:" ,this.Totalcreditworthyscore1);
             return this.Totalcreditworthyscore1

     const Totalcreditworthyscore1 = await(this.docRef)


    
            } else {
                 console.log("No such document!");
             }
    }).catch((error) => {
        console.log("Error getting document:", error);
    
    });
}
     console.log("Kayodes Document data:" ,this.Totalcreditworthyscore1); 

Source: Ask Javascript Questions

LEAVE A COMMENT