0

in node js i have this code

const scraper = () => axios.get("https://thehackernews.com/").then((urlData) => {
  const $ = cheerio.load(urlData.data)
  videoTitle = $(".home-title")
})

console.log("logging to the console:", videoTitle)


client.messages
  .create({
    body: `Message: ${videoTitle}`,
    
    
  })
  .then(message => console.log(message.sid))
  .done();

im trying so what ever is scraped from the website using videoTitle is going to go into the body for the messages, but it keeps saying undefined

evan_tech1234
  • 123
  • 12
  • 1
    You have to put `client.messages.create` and `console.log` into your `then`, or put them into a function called by `then` so that the promise resolves before you try to use them. Sync code doesn't pause and wait for async code to complete--that's the whole point of async. – ggorlen Aug 24 '22 at 00:16
  • 1
    This will be sort of dirty since someone closed the question but here is a bit of code that you could try: `import axios from "axios"; import cheerio from "cheerio"; axios .get("https://thehackernews.com/") .then((urlData) => { const $ = cheerio.load(urlData.data); const videoTitle = $(".home-title"); return videoTitle.text(); }) .then((videoTitle) => { console.log("logging to the console:", videoTitle); // ... client.messages code here }) .catch((error) => { console.log(error.message || error); });` – PCDSandwichMan Aug 24 '22 at 00:21

0 Answers0