0

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)
  • 1
    How exactly is that running in Node? Normally there's not a `window` object; are you using some kind of DOM library? Or is the code you posted actually browser-side code? – Pointy Aug 24 '21 at 13:29
  • why not move the console.log after the push? – depperm Aug 24 '21 at 13:30
  • Im not 100% sure, I followed a video tutorial on how to setup node, there is another JS file that handles the node stuff I belive, I can post it if you need it.(going to post it anyways) – ColtonLuvsComputers Aug 24 '21 at 13:31
  • 1
    You should probably redact that API key. – Andy Aug 24 '21 at 13:59
  • These two questions should help: https://stackoverflow.com/questions/23667086, and https://stackoverflow.com/questions/14220321. – Take-Some-Bytes Aug 24 '21 at 16:17

1 Answers1

0

Your console log is executing before the items are added to the array. Add the console log in the ajax success function or in the displayData function after the push.

iiliev
  • 51
  • 1
  • 4