Reputation: 11753
I have some issue writing typescripe code for a firebase cloud function. I assume it is mainly a syntax problem.
The code is below, but the relevant part is the what concerns the call of the myLocalFunc function. The rest is only here to provide some context. The part reading:
response:Response<any>
in:
const myLocalFunc = (mail:string, flag:boolean, response:Response<any>) => {...}
is wrong. Because I get this error message:
error TS2315: Type 'Response' is not generic.
What is the proper syntax?
const myLocalFunc = (mail:string, flag:boolean, response:Response<any>) => {
admin.auth().getUserByEmail(mail)
.then(function(userRecord) {
// Do some useful work.
const data = {
boolField: flag,
};
const refStr = "/Users/"+userRecord.uid;
admin.database().ref(refStr).set(data);
})
.catch(function(error) {
console.log("Error fetching user data:", error);
let rspMsg = "This user (";
rspMsg += mail;
rspMsg += ") does not exists.";
response.send(rspMsg);
});
};
exports.myFunc = functions.https.onRequest(function(req, resp) {
resp.set("Access-Control-Allow-Origin", "*");
resp.set("Access-Control-Allow-Methods", "GET, POST");
corsHandler(req, resp, async () => {
const from = String(req.body.from);
const idToken = String(req.body.token);
admin.auth().verifyIdToken(idToken)
.then(function(decodedToken) {
const uid = decodedToken.uid;
const refStr = "/Users/"+uid;
const ref = admin.database().ref(refStr);
ref.on("value", function(snapshot) {
console.log("snapshot.val():", snapshot.val());
if (snapshot.val() !== undefined) {
const snv = snapshot.val();
if (snv.adminRights !== undefined) {
if (snv.adminRights) {
// Only if we reach this point,
// can we perform the operation next line.
myLocalFunc(from, true, resp);
}
}
}
}, function(errorObject) {
console.log("The read failed: " + errorObject);
});
}).catch(function(error) {
// Handle error
functions.logger.log("(FL)error:", error);
console.log("(CL)error:", error);
});
}); // End corsHandler.
});
Note:
I got the idea of trying Response<any> for the type (without much conviction) after reading some some documentation for functions.https.onRequest.
If I change the code to:
const myLocalFunc = (mail:string, flag:boolean, response) => {...}
which is in fact what I started with.
I get this error:
error TS7006: Parameter 'response' implicitly has an 'any' type.
If I try to change the code to this:
const myLocalFunc = (mail:string, flag:boolean, response:Response) => {...}
I get these two errors:
37:18 - error TS2339: Property 'send' does not exist on type 'Response'.
37 response.send(rspMsg); // This works.
~~~~
79:46 - error TS2345: Argument of type 'Response<any>' is not assignable to parameter of type 'Response'.
Type 'Response<any>' is missing the following properties from type 'Response': headers, ok, redirected, statusText, and 9 more.
79 myLocalFunc(from, true, resp);
Upvotes: 2
Views: 601
Reputation: 50830
The req
and res
parameters in onRequest()
are Request and Response objects from Express. When not importing Response from Express, it's another interface which also is not generic.
import {Response} from "express";
const myLocalFunc = (mail: string, flag: boolean, response: Response) => {...}
Upvotes: 2