2

I'm using Async/Await in a firebase function as follows

const functions = require("firebase-functions");
const puppeteer = require('puppeteer');

async function getHtml(url) {
    const browser = await puppeteer.launch({
        headless: true,
        args: ['--no-sandbox', '--disable-setuid-sandbox',]
    });

    const page = await browser.newPage();

    await page.goto(url,
        { waitUntil: ['networkidle0', 'networkidle2', 'load', 'domcontentloaded'] });
    const k = await page.content()

    await browser.close();
    return k
};

// Create and deploy your first functions
// https://firebase.google.com/docs/functions/get-started

exports.api = functions.runWith({ memory: '512MB' })
    .https.onRequest((request, response) => {
        // console.log(request.query.url)
        functions.logger.info("Hello logs!", { structuredData: true });
        getHtml(request.query.url)
            .then(function (res) {
                response.send(res);
                console.log(res)
            })
            .catch(function (err) {
                response.send(err);
            })

    })
    ;

This fails the pre-deploy with the error Parsing error: Unexpected token function for the line async function getHtml(url) {

I just assumed that it's an issue with the eslint which is why I removed the predeploy "predeploy": [ "npm --prefix \"$RESOURCE_DIR\" run lint"] in the firebase json which helped me deploy successfully but returned an empty response on testing out the deployed code.

Everything on my local system works flawlessly with firebase serve

Any help would be much appriciated!


Edit:

Steps to reproduce

  • run firebase init in a directory
  • Select functions... , Javascript and (optionally)select no for eslint
  • install puppeteer using npm and paste the code above in the /functions/index.js file
  • run firebase serve and test the code with a url parameter on your local system. This will respond with a rendered html page.
  • Run firebase deploy

Now, on going to the hosted URL and appending a url parameter should just return {} instead of the html

Vayun
  • 95
  • 1
  • 10
  • 2
    How did you create your Firebase project? Please edit the question to give exact steps to reproduce this behavior, starting with project creation, so we can duplicate what you've done so far. – Doug Stevenson Feb 06 '23 at 15:57
  • I simply used `firebase init` ; I've mostly just followed the docs https://firebase.google.com/docs/cli Replying here and not editing my post as I do not think that the project creation was a problem. – Vayun Feb 06 '23 at 16:00
  • The configuration that your project is using is created by the project init process. Either we need to be able to reproduce that result using the instrucitons you give, or you should show the entire configuration of your project including all the files that it created for you. – Doug Stevenson Feb 06 '23 at 16:10
  • 2
    As an aside, `browser.close()` should be in the `finally` block of a `try`-`catch`-`finally` otherwise it will result in a memory leak if you invoke `getHtml` multiple times and one of its steps throw an error while loading the page. – samthecodingman Feb 06 '23 at 17:22
  • @DougStevenson I've added it to the post – Vayun Feb 07 '23 at 05:42
  • @samthecodingman Interesting, I assumed that those could be omitted in async functions. Could you please suggest the edit? – Vayun Feb 07 '23 at 05:43
  • See if this helps with your linting error ~ [eslint Parsing error: Unexpected token function with async](https://stackoverflow.com/q/50285821/283366) – Phil Feb 07 '23 at 05:54
  • @Phil I did try the solutions mentioned without any success. Ended up making a project that does not use Eslint. Regardless, my primary concern is with getting the desired response. – Vayun Feb 07 '23 at 08:40
  • What do the functions logs say? – Doug Stevenson Feb 07 '23 at 13:16
  • @DougStevenson It's quite vague; doesn't really explain much `Function failed on loading user code. This is likely due to a bug in the user code. Error message: Error: please examine your function logs to see the error cause: https://cloud.google.com/functions/docs/monitoring/logging#viewing_logs. Additional troubleshooting documentation can be found at https://cloud.google.com/functions/docs/troubleshooting#logging. Please visit https://cloud.google.com/functions/docs/troubleshooting for in-depth troubleshooting documentation.` – Vayun Feb 07 '23 at 14:46
  • Did you follow those instructions? – Doug Stevenson Feb 07 '23 at 14:52
  • 1
    Of course! The "detailed logs" also had the exact same error message whithout any further details. The troubleshooting docs did not help either – Vayun Feb 07 '23 at 15:07

0 Answers0