Home > OS >  Why Axios don't wanna working in function?
Why Axios don't wanna working in function?

Time:11-19

I wanna create node js scraper. Thanks for Shubham Khatri helping! But i get new suggestion, i trying get 'value' from '.then' and it send me 'value is not defined'. Please says me how to fix this? I trying and don't find how to fix this!

const axios = require('axios')
const cheerio = require('cheerio')
const url = 'XXX'
GetInfo = function() {
  return axios.get(url)
    .then(response => {
        const Response = response.data
        const $ = cheerio.load(Response)
        const text = $('span.bookbuy').text()
        return text;
    })
}
GetInfo().then((value) => console.log("Price today is:"   value));
const BuyPrice = value

I try using 'value' in 'const BuyPrice', but its don't wanna working and send me error 'ReferenceError: value is not defined'. I created scraper to get info of price book, if there is a discount, i go to buy this book.

CodePudding user response:

You can only use the argument value INSIDE that callback for both scope and timing reasons:

GetInfo().then((value) => {
    console.log("Price today is:"   value);
    // use value here
    // insert code that uses it here or call a function and pass it value
});

// Can't use value out here
  • Related