0

I'm making an api call to gather data on a customer, and then saving specific key:value pairs into a global variable.

What's confusing me is that when I console log the object I'm getting back from the api, I'm able to see said data. However, when this data is saved to this global variable (maildata), all key:value pairs are blank.

Here is what my function looks like. This is a vuejs/javascript project.

'''

getAllData(obj) {
        let params = new URLSearchParams();
        let thisvue = this;
        let url = this.$store.state.url;
        let newObj = JSON.stringify(obj);

        params.append('ty', 'allpdf');
        params.append('prikey', newObj);
        axios({
                method: "post",
                url: url,
                data: params
            })
            .then((response) => {
                
                console.log(response.data); //I am able to see object data with this
                thisvue.maildata=response.data; //Saved response.data into maildata global variable
                console.log(thisvue.maildata); //Unable to see data when saved to this variable
                
             })
             .then(() => {
                   thisvue.maildata.pay=JSON.parse(thisvue.maildata.pay);
                   thisvue.maildata.deductions=JSON.parse(thisvue.maildata.deductions);
                   thisvue.maildata.exemptions=JSON.parse(thisvue.maildata.exemptions);
                   thisvue.maildata.explanations=JSON.parse(thisvue.maildata.explanations);
             })
        
}'''
zanzibar
  • 1
  • 1
  • I'm not sure of my answer but the problem for me is that axios returns a promise. At the time of console.log the result, the promise data is still waiting. You need to add async before your function declaration and add await before the call to axios. After that, if you console.log the result, you should be able to see it. Another thing, you should not affect the result of your query in variables inside the promise. Instead, you should do something like this: const data = await axios()... This way you can be sure that the promise will be resolved before you assign the result inside a variable – FeckNeck Apr 25 '23 at 18:49

0 Answers0