I've got a cloud function as follows:
// const {onSchedule} = require("firebase-functions/v2/scheduler"); // The Cloud Functions for Firebase SDK to set up triggers and logging.
import * as logger from "firebase-functions/logger";
const {onRequest} = require('firebase-functions/v2/https');
exports.dailyShiftNotification = onRequest(async (req, res) => {
const promise = fetch(`http://localhost:3000/api/messaging/dailyShiftNotification`, {
method: 'POST',
}).then(res => {
if (!res.ok) {
logger.error("There was an error sending messages");
return res.status(500).send('Error sending messages');
}
return res.json();
}).then(result => {
return result;
});
try {
const result = await promise;
console.log(result);
return res.status(200).send('Messages sent!');
} catch (error) {
console.error('Error:', error);
return res.status(500).send('Error sending messages');
}
});
I can't return a response from my function, as res.status()
this expression is not callable. Type 'Number' has no call signatures
I've tried import { Request, Response } from 'express';
and setting res: Response, req: Request
which does nothing. Installing node-fetch
didn't change anything. I've even just tried straight up ignoring the error with //ts-ignore
. Every time, even though the function is successful, all it does is time out at the end because I can't send an actual response.
The issue you are experiencing as Phil commented is coming from the res shadowing you do with the then(). I would however change the general formatting of this and the way it is working to something like the following :)
try {
const response = await fetch(... rest of tings) // NO THEN
if(!response.ok) { /* error handling you were doing in then */ }
const result = await response.json();
return res.status(200).send('Messages sent!');
} catch (e) {
logger.error('Error:', e);
return res.status(500).send('Error sending messages');
}