How do I return the value within a . then in Vue JS?

Asked

Viewed 48 times

0

I’m new to Vue and Firebase, I have the following problem: I want to return the generated value inside then, in the console.log that is inside then returns the id that was generated, but when I refer the same id outside then I get a Undefined.

  enviarDados(){
    db.collection('teste').add(this.user)
    
    .then((docRef) => {
        this.idGerado = docRef.id
        console.log('Meu id:', this.idGerado)
    })
    .catch((error) => {
        console.error("Error adding document: ", error);
    }); 

    console.log(this.idGerado)

  }
  • There are quite duplicate of this question here on the site. Basically, Promise is used to get a value "in another timeline", so you will only be able to get the value within the then. To solve this, you can use callback functions to work on top of docRef.id.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.