Reputation: 1
ReferenceError: options is not defined at Object. (C:\Users\khair\Documents\GitHub\WideDiscord\index.js:171:9) at Module._compile (internal/modules/cjs/loader.js:1138:30) at Object.Module._extensions..js (internal/modules/cjs/loader.js:1158:10) at Module.load (internal/modules/cjs/loader.js:986:32) at Function.Module._load (internal/modules/cjs/loader.js:879:14) at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12) at internal/main/run_main_module.js:17:47
Here is the code
var options = {
url: "http://results.dogpile.com/serp?qc=images&q=" + "cat",
method: "GET",
headers: {
"Accept": "text/html",
"User-Agent": "Chrome"
}
};
}
request(options, function(error, response, responseBody) {
if (error) {
return;
}
$ = cheerio.load(responseBody);
var links = $(".image a.link");
var urls = new Array(links.length).fill(0).map((v, i) => links.eq(i).attr("href"));
console.log(urls);
if (!urls.length) {
return;
}
// Send result
message.channel.send( urls[Math.floor(Math.random() * urls.length)]);
});
Upvotes: 0
Views: 6487
Reputation: 89
I believe you're trying to follow along with this video https://www.youtube.com/watch?v=EFtTTCbGwYY&ab_channel=CodeLyon
Look carefully, he defined options
and request
under the same function called image
Upvotes: 1
Reputation: 49
You have a "}" beneath the options Object definition, which tells me options is being defined within a function, so even though it's a var, it's undefined outside of whatever function it's in.
To fix this, call requests() within the same scope as options's definition, so requests() will see the data within options.
Upvotes: 1