I can run the local code, but not on Heroku

Asked

Viewed 62 times

0

When I run this code locally everything goes well, and is returned to the Instagram page. However, when I deploy to Heroku I can no longer execute the code because an error appears. I tried to change the version of Node on Heroku, putting exactly the same one I used to run the code on PC but nothing helped.


const request = require('request-promise');
const cheerio = require('cheerio');
/* Create the base function to be ran */
const start = async () => {
    /* Here you replace the username with your actual instagram username that you want to check */
    const USERNAME = 'doarda';
    const BASE_URL = `https://www.instagram.com/${USERNAME}/`;
    /* Send the request and get the html content */
    let response = await request(
        BASE_URL,
        {
            'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
            'accept-encoding': 'gzip, deflate, br',
            'accept-language': 'en-US,en;q=0.9,fr;q=0.8,ro;q=0.7,ru;q=0.6,la;q=0.5,pt;q=0.4,de;q=0.3',
            'cache-control': 'max-age=0',
            'upgrade-insecure-requests': '1',
            'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36'
        }
    );
    
    /* Initiate Cheerio with the response */
    let $ = cheerio.load(response);
    
    /* Get the proper script of the html page which contains the json */
    let script = $('script').eq(4).html();
    console.log(script)
    /* Traverse through the JSON of instagram response */
    let { entry_data: { ProfilePage : {[0] : { graphql : {user} }} } } = JSON.parse(/window\._sharedData = (.+);/g.exec(script)[1]);
    
    /* Output the data */
    console.log(user);
    debugger;
}

start()

The mistake I get on Heroku is this

2020-10-17T01:11:47.767189+00:00 app[worker.1]: (node:4) UnhandledPromiseRejectionWarning: TypeError: Cannot read property '1' of null
2020-10-17T01:11:47.767190+00:00 app[worker.1]: at start (/app/igbot.js:28:128)
2020-10-17T01:11:47.767273+00:00 app[worker.1]: (node:4) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)

Any idea what might be going on?

  • For console.log(script) you will probably see that the local log is different from the server when running g.exec(script) returns null, and when trying to access the property/index 1, causes the error

  • then, but what causes this difference? any idea of why locally I can run, but in Heroku no?

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.