Netlify Functions returns readable stream to browser

Hi There,

I am struggling a bit with functions. My function at ‘/api/getSections’ looks like this:

exports.handler = async function(event, context) {
    
    let fileData = await this.getAllMetaData();
    
    return {
        statusCode: 200,
        body: JSON.stringify(fileData)
    };
}

When running netlify dev, netlify:functions invoke I get the response I need but when moving into the browser I see SyntaxError: Unexpected token < in JSON at position 0.

My fetch request looks like:

const getSections = () => {
    fetch('/api/getSections')
      .then(response => response.json())
      .then(data => console.log(data));

If I log out the response I see:

I have been through this topic in detail: Netlify Dev Functions Locally troubleshooting - but it hasn’t helped. I am hoping there is something small I am missing. I haven’t been able to find any clues as all the documentation seems to indicate this should work without configuration. I am using next ^10.0.1 and static export. If I try this function on a branch deploy I get the same result.

Any guidance on getting this to work in any environment other than the CLI in order to troubleshoot from there would be greatly appreciated.

Warm Regards,
Nick

Hey Nick,
What I think is happening (but am not 100% sure to be honest) is that netlify dev (written in Node) simulates a function call but does not make a call to an actual AWS function, so regular capabilties of Node like reading from streams work locally… but do not work when deployed, since AWS lambdas does not support streaming responses :frowning: Sorry to be the bearer of bad news. If you learn otherwise, or if I misunderstood the problem, I would love to know. This thread has more discussion on functions and streams: