Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
79 views
in Technique[技术] by (71.8m points)

javascript - Pass value to another variable inside a promise doesn't work

I have a component in my application using Vue.js and Firebase and in the created() lifecycle, i'm getting an id that I saved in LocalStorage to use in a promise to get() data in my firestore database. The get() data filter by the id works fine but inside the promise i want to pass the value that i got to another variable, but the value changes with NAN in some fields, you could see in the two console.log() below. I just want to keep the same value doc.data.bdCalculo in variable self.bdCalculo (needed to use self because the this doesn't work):

created: function(){
                // get the id in localstorage
                let id_di = localStorage.getItem('id_di');
                let self = this
                
                var docRef = db.collection("dis").doc(id_di);

                docRef.get().then(function(doc) {
                    if (doc.exists) {
                       var  diFinal = doc.data();

                console.log("Data that comes from Firestore:", doc.data().bdCalculo);

   // The data in firestore comes with some arrays called Step1, Step2, Step3, Step4 and bdCalculo
                 self.step1 = diFinal.Step1;
                 self.step2 = diFinal.Step2;
                 self.step3 = diFinal.Step3;
                 self.step4 = diFinal.Step4;
                 self.bdCalculo = diFinal.bdCalculo; 
                 console.log("Data after pass the value to self.bdCalculo", self.bdCalculo);
               
                
                // pass the valeu of self.bdCalculo to a method in vue.js
                 self.calcCustoFob(self.bdCalculo);
           
                } else {
                console.log("No such document!");
                }
                }).catch(function(error) {
                    console.log("Error getting document:", error);
                });
            },

The data in the first console.log is:

enter image description here

The data after pass the value to self.bdCalculo in the second console.log(): enter image description here

question from:https://stackoverflow.com/questions/65840710/pass-value-to-another-variable-inside-a-promise-doesnt-work

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...