0

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>
isherwood
  • 58,414
  • 16
  • 114
  • 157
Quốc Cường
  • 417
  • 4
  • 12

2 Answers2

1

Try using callback to see the log

.then(res=>{

        // your code as it is

      },()=>{
          console.log(this.arr_link);

          console.log(this.arr_link[0]);
})
.catch(err=>console.log(err));
Atiqul Alam
  • 181
  • 4
0

I tested using fetch. You are not setting the data properly. Assign the res.items to result instead of res.data.items

.then(res => {
    var result = res.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);
      }
    }
  console.log(arr_link); // Still can show it
  console.log(arr_link[0]);
  })

var arr_link = [];
(async () => {
  fetch("https://api.rss2json.com/v1/api.json?rss_url=https://thanhnien.vn/rss/viet-nam.rss")
    .then((response) => response.json())
    .then((res) => {
      var result = res.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
          };
          arr_link.push(link_obj);
        }
      }
    console.log(arr_link); // Still can show it
    console.log(arr_link[0])
    })
    .catch((err) => console.log(err))
  ;
})();
M A Salman
  • 3,666
  • 2
  • 12
  • 26