在 firebase 云函数中调用 TypeScript 函数时出错

Error calling a TypeScript function in firebase cloud function

我在为 Firebase 云函数编写打字代码时遇到了一些问题。 我认为这主要是语法问题。

代码在下面,但是相关的部分是关于myLocalFunc函数的调用。其余的只是在这里提供一些上下文。阅读部分:

response:Response<any>

在:

const myLocalFunc = (mail:string, flag:boolean, response:Response<any>) => {...}

错了。因为我收到此错误消息:

error TS2315: Type 'Response' is not generic.

正确的语法是什么?

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.
});

注:

在阅读了 functions.https.onRequest.

的一些文档后,我有了尝试 Response 类型的想法(没有太多说服力)

如果我将代码更改为:

const myLocalFunc = (mail:string, flag:boolean, response) => {...}

这实际上是我的起点。

我收到这个错误:

error TS7006: Parameter 'response' implicitly has an 'any' type.

如果我尝试将代码更改为:

const myLocalFunc = (mail:string, flag:boolean, response:Response) => {...}

我收到这两个错误:

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);

onRequest() 中的 reqres 参数是来自 Express 的 Request and Response 对象。当不从 Express 导入 Response 时,它​​是另一个也不是通用的接口。

import {Response} from "express";

const myLocalFunc = (mail: string, flag: boolean, response: Response) => {...}