1

I'm trying to send a 200 - Success back using the 'res' parameter of a google cloud function. I kept getting the error 'TypeError: res.setStatusCode is not a function.' even though this should be a part of the object. So I did the below to make sure.

const functions = require('@google-cloud/functions-framework');

functions.cloudEvent('app', (req, res) => {
    
    console.log(typeof res); // "Function"
    console.log(Object.keys(res)); // []
    console.log(res.statusCode); // undefined
    console.log(res.headers); // undefined
    console.log(res.body); // undefined
    
    res.setStatusCode(400);
    res.send('Success');
    
});

It appears res is actually a function (not an object) and statusCode is not defined. Is this correct or Am I missing something here? How do I change this to send a 200 while using @google-cloud/functions-framework? Thank you.

DbxD
  • 540
  • 2
  • 15
  • 29
  • 1
    idk anything about this but i searched on [github](https://github.com/search?q=functions.cloudEvent+language%3AJavaScript&type=code&l=JavaScript) and [this](https://github.com/OpenFunction/functions-framework-nodejs/blob/edb1a97868b628054c49ba0ec48e0603bcbc0887/test/conformance/function.js#L14) does `functions.http` to get access to req and res – cmgchess May 01 '23 at 03:01
  • 1
    @cmgchess yeah, that library is really poorly documented but from what I can see in the types, those are different exports for different purposes – Zac Anger May 01 '23 at 05:38
  • Yeah. I managed to get this done with functions.http. I'll update an example answer. Thank you for helping out. – DbxD May 01 '23 at 21:41

2 Answers2

1

functions-framework is built on top of Express, and their req/res objects wrap Express's, so it should be res.status, not res.setStatusCode.

Additionally, you're mixing the cloudEvent API with the http API. If you want to directly work with the request and response objects, it looks like you'll need to use cloudevents — I'm unable to find anything in the functions-framework repo that points to a better option, but I would welcome a correction on that.

Also there's no need to manually set a 200, because that's the default.

Zac Anger
  • 6,983
  • 2
  • 15
  • 42
  • Thanks, Zac, It still gives an error, 'TypeError: res.status is not a function' (or 'TypeError: res.send is not a function'). I think this is due to res being a function. That is where I'm actually stuck. – DbxD May 01 '23 at 01:30
  • 1
    Oh of course, I see, let me update my answer – Zac Anger May 01 '23 at 01:44
  • Thanks again Zac. Same with me. I also can't find anything in the functions-framework to achieve this. Will update here if I get an answer. – DbxD May 01 '23 at 02:44
0

As @cmgchess suggested, this was achieved by using functions.http. I'm adding an example code block for anyone who may get the same problem.

const functions = require('@google-cloud/functions-framework');

functions.http('app', (req, res) => {
   //Do your work
   res.status(200).send('Success');
});
DbxD
  • 540
  • 2
  • 15
  • 29