I am trying to build a simple website. Simply use vuejs to display data after retrieving. But then I encountered a problem, specifically as follows:
I have retrieved and manipulated data. All data after processing is put into an array. After that, I can still display the result array, but when accessing each element in that array, the result is 'undefined'. I do not know why.
What have I tried
Manipulation via an intermediate variable. The result is still inaccessible to the elements in both arrays.
Change the order of console commands.
My code
<script>
const axios = require('axios');
export default {
data: function() {
return {
arr_link: [],
}
},
created() {
axios.get('https://api.rss2json.com/v1/api.json?rss_url=https://thanhnien.vn/rss/viet-nam.rss', {
params: {
api_key: 'my_api_key',
count: 50
}
})
.then(res=>{
var result = res.data.items;
var char_check = /(Covid|corona|Bệnh nhân số|Phong tỏa|cách ly|bệnh nhân thứ|ncov|Covid-19)/i;
for (var i = 0; i < result.length; i++) { // For loop through the data then
if (char_check.test(result[i].title)) { // reate an object to push inside
var get_link = result[i].link; // arr_link array.
var get_text = result[i].title;
var link_obj = {id: i, src: get_link, text: get_text};
this.arr_link.push(link_obj);
}
}
})
.catch(err=>console.log(err));
console.log(this.arr_link); // Still can show it
console.log(this.arr_link[0]); // Undefined result, I have also tried with this.arr_link[1] or another index.
}
}
</script>