0

I keep getting an error in a simple for loop trying to insert value from one array to another. My JS code is as:

var myApp = angular.module('myApp', []);
myApp.controller('UserCtrl', function($http) {
    let vm = this;
    var datetime = [];
    var venuename = [];
    var venuecity = [];
    var venuecountry = [];
    var url = [];

    var arr = [];
    var id = "secret key";
    vm.searchText;
    var i=0;

    vm.getByID = function() {

        $http.get("https://rest.bandsintown.com/artists/" + vm.searchText + "?app_id="+id)
        .then(response => {
            console.log(response.data)
            vm.name = response.data.name
            vm.thumb = response.data.thumb_url
            vm.fb = response.data.facebook_page_url
            vm.up = response.data.upcoming_event_count
            $http.get("https://rest.bandsintown.com/artists/" + vm.searchText + "/events" + "?app_id="+id)
            .then(response2 => {
                console.log(response2.data)
                vm.arr = response2.data
                console.log(vm.up)
                console.log(vm.arr[0].datetime)
                console.log(vm.arr[1].datetime)
                for (i = 0; i <=(vm.up-1); i++){
                    vm.datetime[i] = vm.arr[i].datetime;
                    vm.venuename[i] = vm.arr[i].venue.name;
                    vm.venuecity[i] = vm.arr[i].venue.city;
                    vm.venuecountry[i] = vm.arr[i].venue.country;
                    vm.url[i] = vm.arr[i].url;
                }
                console.log(vm.up)
            })
        })
    }
});

The datetime part of the arr returns just as i want it to. I just cant store it into the new array. What im trying to do is that the array 'arr' contains all the objects of concerts of an artist. I want to display the individual details, for that I need to store each element in a new array, and repeat to create and show separate event details. Im a newbie at this and any help would be appreciated. Thanks in advance!

georgeawg
  • 48,608
  • 13
  • 72
  • 95
Zaeem Habib
  • 85
  • 1
  • 6
  • 2
    One of the best things you can do early on is learn to use the debugger built into your browser. Learn how to set breakpoints and go through the code line by line and inspect variables. In this case, while going through the code, you'd find that `vm.datetime` is undefined. `datetime` is defined, but not `vm.datetime`. You'd need to change your declaration to `vm.datetime = [];` in order for that code to work. – Heretic Monkey Mar 20 '20 at 17:06
  • Hi, thank you so much, this worked, but can you explain why this error was not caused on the arr? I defined it as plain arr, but used it as vm.arr and it worked perfectly. Thank you again! – Zaeem Habib Mar 20 '20 at 17:10
  • 1
    Because of this line: `vm.arr = response2.data` that line simultaneously defines the `vm.arr` attribute and sets its value to `response2.data`. Your `var arr = [];` was completely ignored. – Heretic Monkey Mar 20 '20 at 17:12

0 Answers0