0👍
The error you have is a network error, saying either you dont have internet connection or the endpoint is unreachable.
Are you trying to connect to a valid endpoint?
You can set up your endpoint in axios like that:
export const apiParams = {
baseURL: 'YOUR URL HERE',
timeout: 20000,
withCredentials: true
}
const api = axios.create(apiParams)
After exporting you can easily use
api.post('db.php', { data });
Basically the unhandled promise rejection is about to saying that axios.post is a promise, and if it is rejected you should handle its error.
Try to put axios.post in a try catch block and you can use ES6 async/await like that:
methods: {
async fetchAllData() {
try {
await axios.post('db.php', {
action: 'fetchall'
})
app.allData = response.data
} catch (error) {
console.log(error)
}
}
},
or you can use the old fashion way error handling with catch, but I don’t recommend to use it, if you have a chain of promises, is better to use async/await as the example above.
methods: {
fetchAllData() {
axios
.post('db.php', {
action: 'fetchall'
})
.then(function(response) {
app.allData = response.data
})
.catch(err => {
console.log(err)
})
}
},
Read about promise handling with then/catch
https://javascript.info/promise-error-handling
Async await:
https://javascript.info/async-await
Until you resolve the network error, the promise rejection should be gone, but you will have a handled error on your console log.
- [Vuejs]-How do I initialise state in Nuxt.js which includes localStorage while Universal Rendering
- [Vuejs]-How to restrict VueX store access to certain pages