flagged as issue with asynchronous call, however call completes prior to final few lines of code, do not understand how that caused the issue?
I am a beginner to Javascript attempting to code a bot for discord. I have encountered another problem that I have spent days trying to fix. Please can someone tell me why my code is throwing this JSON undefined error?
function search(msg) {
const puppeteer = require('puppeteer');
let scrape = async () => {
const browser = await puppeteer.launch({headless: false});
const page = await browser.newPage();
await page.goto(msg);
const result = await page.evaluate(() => {
let data = []; // Create an empty array that will store our data
let ingredients = document.querySelector("ul li").innerText; // Select the ingredients
let level = document.getElementsByTagName("p").innerText; // Select the level
data.push({ingredients, level}); // Push an object with the data onto our array
return data; // Return our data array
});
browser.close();
return result; // Return the data
};
scrape().then((value) => {
return value; // Success!
});
};
var prep5 = search(prep4); //prep4 is a variable string providing a URL, testing with "https://ffxiv.consolegameswiki.com/wiki/Crab_Oil"
var obj = JSON.parse(prep5);
console.log(obj.ingredients + ' and ' + obj.level);
I see puppeteer successfully open the webpage, then close itself on a completed load, and would expect in this case to see "2 Water Shard, 2 Megalocrab Leg and Level 26 Alchemist" output to the console, however all I get is
"SyntaxError: Unexpected token u in JSON at position 0 at JSON.parse ()"
EDIT: quick link to reference page - https://ffxiv.consolegameswiki.com/wiki/Crab_Oil