Search code examples
javascriptjsonember.jsember-dataember-cli

Access to nested object json in SoundCloud with ember-data


I am working on a soundCloud json for the favorites songs from an user.

You can see it here

enter image description here

I can access to my favorites tracks but i can not access to the user id and username.

Here the code i am using which returns my favorite properties and i have commented the code which is not working to return the user properties.

I get this error in the console "Uncaught TypeError: item.user.forEach is not a function"

What am i doing wrong? is it the right way to access to my user properties?

model: function(params) {
    var artist, favoriteListProxy, self;
    self = this;
    artist = params.artist;
    this.controllerFor('application').set('artistName', artist);
    favoriteListProxy = Ember.ArrayProxy.create({
        content: []
    });
    return new Ember.RSVP.Promise(function(resolve, reject) {
        return SC.get("/users/" + 'mannaio' + "/favorites", {limit: 40}, function(favorites) {
            if (favorites.length) {
                favorites.forEach(function(item, index, arr){
                    var favorite;
                    favorite = self.createFavoritelist(item, favoriteListProxy);
                    // return item.user.forEach(function(user, index, arr){
                    //     return user = self.createUser(user, favorite);
                    // });
                });
                favorites = favoriteListProxy.get('content')
                return resolve(favorites);
            }
        });
    });
},

createFavoritelist: function(favorite, arr) {
    var record;
    record = this.store.createRecord('favorite', {});
    record.setProperties({
        id: favorite.id,
        title: favorite.title,
        artwork_url: favorite.artwork_url,
        genre: favorite.genre
    });
    arr.pushObject(record);
    return record;
},

// createUser: function(user, favorite) {
//     var record;
//     record = this.store.createRecord('user', {});
//     record.setProperties(user).set('favorite', favorite);
//     return record;
// },

Solution

  • It appears to me that item.user is an Object and not an Array. Therefore it doesn't have a forEach method.

    So try:

    return self.createUser(item.user, favorite);