I am trying to create an array of User IDs to send in an AJAX Post Request. I can get the User IDs okay and display them individually in console.log but after I push them to the connectionData
array and try to do a console.log on specific indices I get undefined
, also when I try console.log(connectionData.length)
it returns 0. I believe this is a sync issue but I can't figure out how to solve it, I have read similar posts and they aren't helping.
const prepareUserDataForConnection = function(){
var connectionData = [];
var currentUser = 'robert';
var currentUser = 'chris';
console.log(currentUser);
console.log(followedUser);
$.ajax({
type: 'GET',
url: '/api/currentuserinfo/',
data: {
'current_user': currentUser
},
success: function(data) {
let currentUserId = JSON.stringify(data[0].id);
console.log('Current User Pk is: ' + currentUserId);
connectionData.push(currentUserId);
},
});
$.ajax({
type: 'GET',
url: '/api/followeduserinfo/',
data: {
'followed_user': followedUser
},
success: function(data) {
let followedUserId = JSON.stringify(data[0].id);
console.log('Followed User Pk is: ' + followedUserId);
connectionData.push(followedUserId);
},
});
console.log(connectionData)
console.log(connectionData[0])
console.log("Array length is: " + connectionData.length)
};