0

Why am I getting post undefined if it is decleared in async post first line?

postsJSON = await posts.map(async post => {
    const contentPost = await page.evaluate(async () => {
        const response = await fetch("https://www.instagram.com/p/" + post['shortcode'] + "/?__a=1")
        return await response.json()
    })
    ...
})
md123
  • 295
  • 3
  • 8
  • The `page.evaluate` runs in a *completely separate* scope; it doesn't have access to outer variables unless you pass them into it explicitly. – CertainPerformance Mar 16 '20 at 01:25
  • @CertainPerformance If I do `await page.evaluate(async post) =>`, is that right? Because I'm getting an error. – md123 Mar 16 '20 at 01:27
  • No, that's invalid syntax. Either put parentheses around the argument list, or don't (but don't put parentheses on one side but not the other) – CertainPerformance Mar 16 '20 at 01:28
  • @CertainPerformance Sorry, typo. But that's what I meant. I'm getting an error of `TypeError: Cannot read property 'shortcode' of undefined` – md123 Mar 16 '20 at 01:30
  • @CertainPerformance If I console.log `post['shortcode']` before the `const contentPost`, it works, but it says undefined when called inside the page.evaluate – md123 Mar 16 '20 at 01:33
  • Like I said, the scope of the callback is separate. You need to pass the post to the callback - see the linked question. – CertainPerformance Mar 16 '20 at 01:34

0 Answers0