I apologize if I word this question wrong, I am very new to JS especially involving node. But I do have an issue, so in my code, I defined an array outside of any function, literally the first line of the code with no tabs. When I use playerlist.push(x)
inside of a function and try to do console.log(playerlist[0])
(the array is empty so I know there will only be 1 item in the array) it logs as undefined. The array in question is playerlist. The console.log call that is not working is the second to last line of code.
const playerlist = []
const playerdict = {}
const mtl = []
const queryString = window.location.search;
const evpath1 = queryString.split('?')[1] + '?' + queryString.split('?')[2];
const evpath2 = queryString.replace('?window=', '/').split('?')[1];
var uri = 'https://fortnitetracker.com/events/' + evpath2 + '/watch';
var data = {};
data.uri = uri
$.ajax({
type: "POST",
url: '/',
data : data,
success: function(data){
metaEventId(data);
}
});
function metaEventId(data){
var metaEventId = data.split('metaEventId: ')[1].split(',')[0];
mtl.push(metaEventId)
}
function addplayer() {
var epicName = $('#epic');
var uri = 'https://fortnitetracker.com/api/v0/profiles/find?platformUserHandle='
var data = {};
data.uri = uri + epicName.val().replace(' ', '+');
$.ajax({
type: "POST",
url: '/',
dataType: 'json',
data : data,
success: function(data){
data = JSON.parse(data);
displayData(data);
}
});
function displayData(data){
var epicUserId = data.platformUserId;
var epicUserName = data.platformUserHandle;
playerlist.push(epicUserName);
playerdict[epicUserName] = epicUserId;
var data = {};
}
console.log(playerlist[0]);
}
Here is the node side stuff(I belive)
var express = require('express');
var app = express();
var path = require('path');
var request = require('request');
var bodyParser = require('body-parser');
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use('/public', express.static(path.join(__dirname, 'static')));
app.get('/',function(req,res){
res.sendFile(path.join(__dirname + '/static/index.html'));
});
// TRN-Api-Key: 3a586c4e-57f6-4695-8167-c1ea9467fc84
app.post('/',function(req,res){
request.get(req.body.uri,{
headers : {
'TRN-Api-Key': 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
}},function(error,response,body){
console.log(body);
res.json(body);
});
});
var port = process.env.PORT || 3000;
app.listen(port)