Callback functions

Hi,

I created a lambda function to take a screenshot and return a png/jpg but I struggle to understand how the function return the result.

Here is the function:

function Card(event, context, callback) {
	console.log({ app });
	app.fromURL("https://google.com", null, function(err, buffer) {
		console.log({ err, buffer });
	});
}
exports.handler = Card;

When I run this function locally (with node filname) it works, but when I run it through netlify function, the second console.log is never called and I would like to understand why :thinking: (the function return a 200)

Any idea?

Thanks :slight_smile:

It looks like something fails silently without any log so its really hard to understand what is happening. I tried using another library and it always return a 200 without any of the information I asked :thinking:

const Nightmare = require("nightmare");

async function Card() {
	console.log("Start");
	const nightmare = Nightmare();
	console.log({ nightmare });
	const screenshot = await nightmare
		.goto("https://google.com")
		.screenshot()
		.end()
		.catch(error => {
			console.error({ error });
		});

	console.log({ screenshot });

	return {
		statusCode: 200,
		headers: {
			"Content-type": "image/png"
		},
		body: screenshot.toString("base64"),
		isBase64Encoded: true
	};
}

exports.handler = Card; 

The log “start” and “nightmare” are properly logged, but then nothing and the function returns a 200 :thinking: Really disturbing. (And its working locally when I manually call Card()

Personally, to debug, I would break the chain you have down after await nightmare and console.log each step to see where it’s getting to.

Are each of goto, screenshot and end returning promises? If so, I’d await each with a console.log of some text and the result between each call.

Also, you’re using a catch callback within an async function when you could wrap it in a try/catch block for more clarity.

1 Like

Thanks for chiming in @ross.coundon- I think those are good suggestions. @Gbreux I’m also wondering if you can share your Netlify URL so we can take a closer look at your setup, check out network requests in the console, etc. Could you also please share an x-nf-request-id from a function call (if it exists, of course- it’s not clear at this point that a request is actually being made, but if one is and then errs out, it will help us troubleshoot to have that id)? Info on how to go about finding that here: