I am trying to get "msg" out of this function to do math on it and it keeps coming back undefined. However when within the function I can read the data but cant do math on it.
let getArr = []
const https = require('https')
const options = {
hostname: 'financialmodelingprep.com',
port: 443,
path: '/api/v3/quote/TSLA?apikey=?apikey=demo',
method: 'GET'
}
const req = https.request(options, (res) => {
res.on('data', (data) => {
data = JSON.parse(data)
data.map(( msg ) => {
//console.log(msg)
getArr.push(msg)
})
})
})
console.log(getArr.slice(-1)[0])
question from:
https://stackoverflow.com/questions/65865538/why-is-my-array-coming-back-undefined-outside-of-the-function 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…