apnp_apnu
apnp_apnu

Reputation: 23

why am i getting: Uncaught ReferenceError: response is not defined

Why am i getting this ? and how can i fix it? i m fairly new to coding: i've tried to fix it but i dont know how: here is the code:

ive tried everything i know how to do, unfortunately my knowledge is very sparse, i have taught myself everything so im not fully aware of all the lingo, the website is beatsbycayde.com, essentially im trying to be able to get the character id so i can then use it to link to braytech.org/2/{destinyid]/{characterId}/legend

    // get list of members and populate roster table

var roster = [];

$.when(
		$.ajax({
		url: "https://www.bungie.net/platform/GroupV2/699392/Members/",
		headers: {
			"X-API-Key":"47b810e692d64237911c2cbe0d433cfe"
		}
	})
	.success(function(json) {

		if (json.ErrorStatus === 'Success') {

			roster = json.Response.results;

			console.log('Exalted member list:', roster);

		} else {

			alert('Uh oh, looks like Bungie\'s doing server maintenance or having problems. Please check back again soon!');
			console.log(json);

		}

	})
	.error(function(json) {

		alert('Uh oh, looks like Bungie\'s doing server maintenance or having problems. Please check back again soon!');
		console.log(json);

	}),

	$.ajax({
		url: 'https://www.bungie.net/platform/destiny2/2/profile/4611686018429000034/?components=200',
		headers: {
			'X-API-Key': "47b810e692d64237911c2cbe0d433cfe"
		}
	}).success(function(res) {
		console.log('PS4 stats:', res);
	})

)
.then(function() {

	listMembers(roster);

});

function listMembers(rsp) {

  var
  list = $('.memberList-list'),
  on = 0,
  sortMembers = function(method) {
    // sort by date joined
    if (method = joined) {
      list.find('.member').sort(function(a, b) {
        return ($(b).data('joined')) < ($(a).data('joined')) ? 1 : -1;
      }).appendTo(list);
    } else if (method = username) {
      list.find('.member').sort(function(a, b) {
        return ($(b).data('username')) < ($(a).data('username')) ? 1 : -1;
      }).appendTo(list);
    }
    list.find('.member.online').prependTo(list);
  };

  for (var i = 0; i < rsp.length; i++) {

    var
		profile = rsp[i].bungieNetUserInfo,
		member = $('<a></a>');

		// tally up online members
		if (rsp[i].isOnline) {
			on++
		}

		// check for valid profile
		// some users don't have Bungie profiles somehow and it breaks function
    if (typeof profile != 'undefined') {
			// store response data in semantic variables
      var
        name = rsp[i].destinyUserInfo.displayName,
        joinDate = rsp[i].joinDate,
        joined = joinDate.substring(0, joinDate.indexOf('T')),
        online = rsp[i].isOnline,
        icon = profile.iconPath,
        memberId = profile.membershipId,
        memberType = rsp[i].destinyUserInfo.membershipType,
        destinyId = rsp[i].destinyUserInfo.membershipId,
        rank = rsp[i].memberType;
			// configure D OM node and add to page
	     $('#destiny-Id').text(destinyId);
	   
      member
        .attr({
          'class': 'j-row vertical-center-row member',
          'href': '/player/?bungieId=' + memberId + '&destinyId=' + destinyId + '&joined=' + joined + '&rank=' + rank,
          'title': 'See player profile for ' + name,
          'data-joined' : joined.replace(/-/g, ''),
          'data-username': name,
          'data-online' : 'false',
          'data-searchable' : name,
        })
	    
	$.ajax({
		url: "https://www.bungie.net/Platform/Destiny/2/Account/"+ destinyId +"/",
		headers: {
			"X-API-Key":"47b810e692d64237911c2cbe0d433cfe"
		}
	})
	.success(function(response) {

        if (data.ErrorStatus === 'Success') {

            depth = response.data;

            console.log('Exalted member list:', roster);

        } else {

            alert('Uh oh, looks like Bungie\'s doing server maintenance or having problems. Please check back again soon!');
            console.log(json);

        }
        //have the line here
        var 
	CharacterId = depth.characters.characterbase.characterId;
    })
	    .html(
          '<div class="j-col j-col-1 member-icon"><img src="https://bungie.net/' + icon + '"></div>' +
          '<div class="j-col j-col-3 member-name"><h3>' + name + '</h3></div>' +
          '<div class="j-col j-col-3 member-joined" data-label="Joined">' + joined.replace(/-/g, '/') + '</div>' +
          '<div class="j-col j-col-3 member-status" data-label="Status"><span class="member-online" id="status-' + memberId + '">' + online + '</span></div>' +
          '<div class="j-col j-col-3 member-button"><a class="button outline gold full-width">' + 'View Stats' + '</a></div>'+
	     '<div class="j-col j-col-3 members-button"> + <a href="https://braytech.org/2/'+ destinyId +'/' + CharacterId +' /legend">In Depth Stats</a>' + '</a></div>'
	       );

			if (rsp[i].exalted) {
				member.addClass('exalted')
				.attr({
					'href': '/player/?bungieId=' + memberId + '&destinyId=' + destinyId + '&joined=' + joined + '&rank=' + rank + '&exalted=false'
				})
				.find('.member-name').find('h3')
				.html(name + ' &nbsp;<span class="gold" title="Exalted">&epsilon;</span>');
			}

			member.appendTo(list);

			// indicate online/offline status
      if (String(online) === 'true') {
        $('#status-' + memberId)
        .text('Online')
        .addClass('online')
        .closest('.member')
        .attr('data-online', true)
        .addClass('online');
      } else {
        $('#status-' + memberId).text('Offline').removeClass('online');
      }

      sortMembers(joined); // sort members by join date

    }

  }

	$('#member-count').text(on + ' / ' + rsp.length + ' Members Online');

}

Upvotes: 1

Views: 2434

Answers (1)

Anis R.
Anis R.

Reputation: 6922

This line:

var CharacterId = response.characters.characterbase.characterId;

is not inside the function(response) { ... } block, therefore it does not have access to the response variable, hence the "not defined" error.

Try inserting this line inside the block.

Edit: What I meant is:

.success(function(response) {

        if (json.ErrorStatus === 'Success') {

            response = response.data;

            console.log('Exalted member list:', roster);

        } else {

            alert('Uh oh, looks like Bungie\'s doing server maintenance or having problems. Please check back again soon!');
            console.log(json);

        }
        //have the line here
        var CharacterId = response.characters.characterbase.characterId;
    })

//not here
var CharacterId = response.characters.characterbase.characterId;

Upvotes: 1

Related Questions