0

I created a Node Server and app.get('/links') route to grab entire image list. On FrondEnd I'm using standard HTML file with vanilla JavaScript and only $.get() JQuery function. Function perfectly loading data in console but for some reason I'm unable to use this data outside of $.get(). How can i read this data in standard JS Script embeded in HTML? FrontEnd Script Embeded in HTML Code

$.get('/links', (data) => {
            loaddata(data);
        })
    let allImages= []
    let firstImg = ''
    let lastImg = ''
    let totalImgs = 0
    let currentImgIndex = 0    
    function loaddata(inpArr){
        totalImgs = inpArr.length
        firstImg = inpArr[0]
        firstImg = inpArr[totalImgs-1]
        allImages = inpArr
    }

NodeJs Route

var express = require('express')
var fs = require('fs');
var array = fs.readFileSync('links.txt').toString().replace(/\r\n/g,'\n').split("\n");
for(i in array) {
    console.log(array[i]);
}
app = express()

app.use(express.static(__dirname))

app.get('/links', (req, res) => {
    res.send(array)
})

app.listen(3000)

Links File

9th-Chem-2013-G1-011
9th-chem-ch-05-02
9th-chem-ch-05-03
9th-chem-ch-05-04
9th-chem-ch-05-05
Aly
  • 321
  • 1
  • 4
  • 15
  • "Unable" in what way? What specifically isn't working as expected? In your browser's debugging tools, does the AJAX response contain the data you expect? Is the `loaddata` function invoked? When you debug, does that function successfully execute with the expected values? What isn't working? – David Nov 22 '21 at 16:56
  • I can't access data outside the function. Plus I also can't load data in any other variable. they all return empty – Aly Nov 22 '21 at 17:06
  • Where are you trying to access the data outside the function? The code shown sets the values, but never uses them. Please provide a [mcve] which demonstrates the problem. – David Nov 22 '21 at 17:08

0 Answers0