Georgios
Georgios

Reputation: 1037

The view function for 'run' did not return a valid response. The function either returned None or ended without a return statement

I did write an HTTP Cloud Function in the Google Cloud Platform. The purpose of this function is to update data in the Database (Firestore).

I do not intend to return something from this trigger. At least not directly. The updated data is getting fetched with a stream.

Although the Cloud Function does exactly what I want, I get in the logs the following error:

The view function for 'run' did not return a valid response. The function either returned None or ended without a return statement.

Do I always have to return something? What would be good practice for this case?

Update:

In the end I just used the following json object

return '{"status":"200", "data": "OK"}'

I am not satisfied with my solution, but for the time it will do.

Upvotes: 3

Views: 3326

Answers (2)

Froilan C
Froilan C

Reputation: 19

When you work with Cloud Functions, these may create background tasks (threads, futures, Promises, etc) as it seems to be your case. When this happnes you must terminate or resolve these tasks before you return an HTTP response.

Also, you need to make sure that you send an HTTP response when these Cloud Functions are HTTP-triggered.

Failing to the aforementioned may cause unpredictable behavior, latency and some messages in logs like the one you found.

If you want to terminate background or HTTP functions, this code from Google Documentation.

Background functions:

// Await-ing promises within functions is OK if you do not return anything
await Promise.resolve();
 
// These will cause background tasks to stop executing immediately
return 1; // OK: returning a value
return await Promise.resolve(); // WRONG: returning the result of a promise
return await Promise.reject(); // WRONG: same behavior as resolved promises
 
// These will wait until the related background task finishes
return Promise.resolve(); // OK: returning the promise itself
return Promise.reject(); // OK: same behavior as to-be-resolved promises

HTTP Functions

// OK: await-ing a Promise before sending an HTTP response
await Promise.resolve();
 
// WRONG: HTTP functions should send an
// HTTP response instead of returning.
return Promise.resolve();
 
// HTTP functions should signal termination by returning an HTTP response.
// This should not be done until all background tasks are complete.
res.send(200);
res.end();
 
// WRONG: this may not execute since an
// HTTP response has already been sent.
return Promise.resolve();

I hope this information helps you to solve your problem.

Upvotes: 0

Ocean Overflow
Ocean Overflow

Reputation: 571

If a function creates background tasks (in your case it's a Promise object when updating Firestore), you must terminate or otherwise resolve these tasks before returning an HTTP response.

Upvotes: 0

Related Questions