2

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);
8
  • Try response:Response without <any> or can you show where is Response imported from? Commented Feb 4, 2022 at 14:19
  • I suggest editing the question to show the complete, minimal example that reproduces this problem. Show the entire file, and strip out everything that's not directly part of the problem. You should also show your package.json so we can see the versions of libraries you're using. stackoverflow.com/help/minimal-reproducible-example Commented Feb 4, 2022 at 14:39
  • The last parameter of myLocalFunc is coming from the last parameter of functions.https.onRequest(function(req, resp), which is resp. I guess I only need to write its type in the myLocalFunc prototype (or signature). I also added a note at the end of the post, please take a look. Commented Feb 5, 2022 at 0:25
  • I do not import Response from anywhere. I just needed to put a type for the last parameter and did quite know what to put. Commented Feb 5, 2022 at 0:35
  • 1
    Try import { Response } from "express" and then using Response as type there. Commented Feb 5, 2022 at 8:30

1 Answer 1

2

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) => {...}
Sign up to request clarification or add additional context in comments.

2 Comments

I removed the spaces on both sides of "Response" in the first line otherwise it does not work.
That might be your code formatter with some warnings.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.