When I access https://jsonplaceholder.typicode.com/todos?_limit=1 in my browser I get:
[
{
"userId": 1,
"id": 1,
"title": "delectus aut autem",
"completed": false
}
]
I am now trying store the result (and print) of a simple http GET request in a variable using axios in vue.js with the above URL:
const result = axios.get('https://jsonplaceholder.typicode.com/todos?_limit=1');
console.log("result is = " + result)
console.log("result.title is = " + result.title)
But the above gives the below output:
App.vue?234e:59 result is = [object Promise]
App.vue?234e:60 result.title is = undefined
Is it not possible to print a json friendly result from a http get request (like with curl) without having to deal with promises
in vue.js?
UPDATE:
Based on below answers I have now tried the following in my App.vue file
<script>
import axios from 'axios';
export default {
name: 'app',
components: {
AddTodo
},
data(){
return {
todos: []
}
},
methods: {
// not allowed to use function keyword here
async printJson(){
let result = await axios.get('https://jsonplaceholder.typicode.com/todos?_limit=1');
console.log("result is = " + result)
console.log("result.title is = " + result.title)
},
addTodo(newTodo) {
this.printJson()
}
}
}
</script>
But that just gives:
App.vue?234e:50 result is = [object Object]
App.vue?234e:51 result.title is = undefined