I have an issue while writing Firebase cloud functions.
Here is the relevant code I have for a cloud function:
import {onRequest} from "firebase-functions/v2/https";
import * as admin from "firebase-admin";
.....
admin.initializeApp();
exports.myCloudFunc = onRequest({cors: ["*"]}, (req, res) => {
let dbStr:string;
.....
const dbRef = admin.database().ref(dbStr);
dbRef.once("value", function(snapshot) {
const snpsv = snapshot.val();
if ((snpsv === undefined) || (snpsv == null)) {
jsonResult = {"Error": "No data found."};
res.send(jsonResult);
}
const reviewType = snapshot.child("revwType").val();
// Beginning of BIG-CHUNK-OF-CODE
const db2ndRef = admin.database().ref(reviewType);
.....
db2ndRef.once("value", function(snapshot) {
const snpsv = snapshot.val();
.....
});
// End of BIG-CHUNK-OF-CODE
});
}); /* End of myCloudFunc */
The question I have relates to the part of the code, between these two lines:
// Beginning of BIG-CHUNK-OF-CODE
// End of BIG-CHUNK-OF-CODE
The code above works, but instead of having this huge chunk of code, I would like to abstract it away in a function that I would call like this:
// Beginning of BIG-CHUNK-OF-CODE
jobFunc(reviewType, res);
// End of BIG-CHUNK-OF-CODE
The problem is that I can’t figure out the proper prototype for the function:
I tried something like the following, but it does not work:
function jobFunc(dbPath:string, res:Response) {
.....
} /* End of jobFunc */
I get error messages concerning the type of the parameters.
res:Response
looks wrong.
Does anyone know how to solve this ?
Or is something impossible with what I want to do ?