I´m following this example from firebase. https://firebase.google.com/docs/functions/get-started
I´ve just download all the required API and had started learning.
I´m creating the functions with TypeScript, this "Hello World" https://github.com/firebase/functions-samples/blob/master/typescript-getting-started/functions/src/index.ts worked fine.
But "addMessage()" didn't.
exports.addMessage = functions.https.onRequest((req, res) => {
const original = req.query.text;
return admin.database().ref('/messages').push({original: original}).then((snapshot) => {
return res.redirect(303, snapshot.ref.toString());
});
});
It's giving this error message:
Firebase Admin SDK ERROR: Expression has type
void
. Put it on its own line as a statement
This is a bit late, but in case others run into this issue, the problem is that res.redirect
does not return a value, so it's return type is defined as void
.
The error is because the return
statement expects a value. You cannot return void
. Thus, the TypeScript compiler is viewing return res.redirect(303, snapshot.ref.toString());
as return void
, which is what generates the error.
The solution is to put the return on the next line:
res.redirect(303, snapshot.ref.toString());
return;
Unfortunately, while it is invalid TypeScript, best practice is to always put a return before code that finalizes a response, so it doesn't fall through to the rest of the code in a function.
Putting the return on the next line is easy to forget, and easy to miss when scanning code. The return with the response finalization makes it clear the processing is done.
You can disable this by disabling the no-void-expression
linting rule, but that is not recommended, since it's a useful rule in general.
I encountered this eslint error by adding the await
keyword in front of a function that was not defined as async
e.g.
function foo(bar) {
return bar;
}
...
await foo(bar);
Removing await
fixed the error.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With