Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

120
Views
Passing in list elements in javascript to a different function

I'm trying to do a web scraping exercise where some values are being retrieved and are being stored in a list variable. I am then passing in the list variable as a parameter in a different function. The problem with my approach is I am getting an error when calling the different function. I believe I'm getting this error because I am not passing in the list elements into the function appropriately. In the function, I am reading from a Yahoo Stock API used to retrieve stock data. If I were to hardcode a stock symbol into the parameter for the function, it works without any issue. Since I am passing in a parameter, I am getting this error. Below is my code and the error I am getting. Any feedback would be helpful.

Code

const cheerio = require('cheerio');
const axios = require('axios');
const yahooStockPrices = require('yahoo-stock-prices');

var stockSymbol = []
async function read_fortune_500() {
  try {
    const { data } = await axios({ method: "GET", url: "https://en.wikipedia.org/wiki/List_of_S%26P_500_companies", })
    const $ = cheerio.load(data)
    const elemSelector = '#constituents > tbody > tr > td:nth-child(1)'

    $(elemSelector).each((parentIndex, parentElem) => {
      let keyIndex = 0
      if (parentIndex <= 9){
      $(parentElem).children().each((childIndex, childElem) => {
        const tdValue = $(childElem).text()

        if (tdValue) {
          //stockObject[keys[keyIndex]] = tdValue
          stockSymbol = tdValue
        }
      })
      console.log(stockSymbol)
    }
    })
} catch (err) {
    console.error(err)
  }
  return stockSymbol;
}

async function collect_stocks(stockSymbol) {
  stockSymbol = read_fortune_500()
  const stockResult = await yahooStockPrices.getCurrentData(stockSymbol);
  console.log(stockResult);
}

collect_stocks(stockSymbol)

Error

/node_modules/yahoo-stock-prices/yahoo-stock-prices.js:75
                .split('regularMarketPrice')[1]
                ^

TypeError: Cannot read properties of undefined (reading 'split')
    at Request._callback (/node_modules/yahoo-stock-prices/yahoo-stock-prices.js:75:21)
    at Request.self.callback (/node_modules/request/request.js:185:22)
    at Request.emit (node:events:390:28)
    at Request.emit (node:domain:475:12)
    at Request.<anonymous> (/node_modules/request/request.js:1154:10)
    at Request.emit (node:events:390:28)
    at Request.emit (node:domain:475:12)
    at IncomingMessage.<anonymous> (/node_modules/request/request.js:1076:12)
    at Object.onceWrapper (node:events:509:28)
    at IncomingMessage.emit (node:events:402:35)
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

The parameter stockSymbol seems to be empty when you pass it to your desired function therefore, when yahoo-stock-prices try to apply a split on it, it fails.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!