I’ve got a cloud function as follows:
<code>// 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');
}
});
</code>
<code>// 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');
}
});
</code>
// 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.