0

I'm trying to access the promise value from an Axios call from another function.

methods: {
    checkItem() {
        const url = "http://api.crossref.org/journals/" + this.editedItem.issn;
        return axios.get(url)
            .then(response => response.status);
    },

    save() {
        console.log(this.checkItem());
    }
}

I'm expecting the status code to be logged in the console when I call save(). At the moment, it's logging the whole promise.

khan
  • 1,466
  • 8
  • 19
user5067291
  • 440
  • 1
  • 6
  • 16
  • Possible duplicate of [How to access the value of a promise?](https://stackoverflow.com/questions/29516390/how-to-access-the-value-of-a-promise) – khan Jul 24 '19 at 18:33

3 Answers3

2

You can use async/await.

<script>
new Vue({
  el: "#root",

  methods: {
    checkItem() {
      const url = "http://api.crossref.org/journals/" + this.editedItem.issn;
      return axios.get(url).then(response => response.status);
    }
  },
  async save() {
    console.log(await this.checkItem());
  }
});
</script>

Or using Promise:

<script>
new Vue({
  el: "#root",

  methods: {
    checkItem() {
      const url = "http://api.crossref.org/journals/" + this.editedItem.issn;
      return axios.get(url).then(response => response.status);
    }
  },
  save() {
    this.checkItem().then(res => {
      console.log(res);
    });
  }
});
</script>
Andrew Vasylchuk
  • 4,671
  • 2
  • 12
  • 30
0

Change your save() function to the following:

async save() {
  const response = await this.checkItem()
  console.log(response);
  // The status code is likely in something like response.status
}
bart
  • 155
  • 7
0
    this.checkItem().then(res => {
      console.log(res);
    });
ahmed galal
  • 259
  • 2
  • 5