Restful returns Undefined in JS Class

Asked

Viewed 41 times

0

I’m making changes to HTML after taking data from the Lastfm API, working with class in JS, making method calls within the class itself, where the date was called before, but this returns Undefined:

    class Tidal{
        constructor(ev){
            this.dom = ev;
            this.data = {};
            this.elements = {};

            this.getInfo();
            console.log(this)
        }

        getInfo(){
            var self = this.data;

            var lastfm = new LastFM({
              apiKey    : '929cfb240401e3f7c1c0cd531aa1939e',
              apiSecret : 'ac2ecff79d5881a1a8e810f249f052df'
            });

            lastfm.artist.getInfo({artist: 'Madeon'}, {success: function(data){
                self.artistInfo = data;
            }, error: function(code, message){
              console.log(code, message);
            }});

            console.log(self.artistInfo, 12345) // retorna undefined
        }
   }

However when called after the file is loaded in devtools the method works correctly.

  • 1

    It seems to me to be timing problem. See that you are calling the property out of the callback. Outside the callback the property does not exist.

  • My dear, I think the problem is this call of getInfo inside the constructor. I believe you should not invoke it there.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.