尝试使用查询参数卷曲 URL 时,Firebase Cloud Functions 内部服务器错误

问题描述 投票:0回答:1

我正在尝试将一些 URL 参数传递到我的云函数,但当我尝试发出请求时,我一直面临内部服务器错误。有谁知道可能是什么原因造成的?

curl -X POST "https://testactivatelicense-a123bcdefg-uc.a.run.app/" -d license_key=3F9D04E3-F661-4F33-BB22-53FE83F111DC -d instance_name=test_1234 
Internal Server Error%
exports.testActivateLicense = onRequest(async (request, response) => {
  try {
    const licenseKey = request.query.license_key;
    const instanceName = request.query.instance_name;
    logger.log("Received request with licenseKey:", licenseKey, "and instanceName:", instanceName);

    const params = new URLSearchParams({license_key: licenseKey, instance_name: instanceName}).toString();
    logger.log("Constructed query parameters:", params);

    response.status(response.status).send(response.data);
  } catch (error) {
    logAndSendError(error);
  }
});

function logAndSendError(error, res) {
  logger.error("Error occurred:", error.message);

  if (error.response) {
    logger.error("Error response status:", error.response.status);
    logger.error("Error response data:", JSON.stringify(error.response.data, null, 2));
    res.status(error.response.status).send(error.response.data);
  } else {
    res.status(500).send(error.message);
  }
}

function logAndSendError(error, res) {
  logger.error("Error occurred:", error.message);

  if (error.response) {
    logger.error("Error response status:", error.response.status);
    logger.error("Error response data:", JSON.stringify(error.response.data, null, 2));
    res.status(error.response.status).send(error.response.data);
  } else {
    res.status(500).send(error.message);
  }
}

以下是来自 Google Cloud Logs Explorer 的日志:

TypeError: Cannot read properties of undefined (reading 'status')
    at logAndSendError (/workspace/index.js:76:9)
    at /workspace/index.js:104:5
    at /workspace/node_modules/firebase-functions/lib/common/onInit.js:33:16
    at AsyncLocalStorage.run (node:async_hooks:338:14)
    at /workspace/node_modules/firebase-functions/lib/v2/trace.js:18:37
    at /layers/google.nodejs.functions-framework/functions-framework/node_modules/@google-cloud/functions-framework/build/src/function_wrappers.js:98:17
    at process.processTicksAndRejections (node:internal/process/task_queues:77:11)

更新,我已经修复了上述错误,现在我面临一个新问题,似乎我的查询参数没有正确传递给函数。这是来自 GCP Log Explorer:

Received request with licenseKey: undefined and instanceName: undefined

Constructed query parameters: license_key=undefined&instance_name=undefined

这是我最新的

testActivateLicense
供参考:

exports.testActivateLicense = onRequest(async (request, response) => {
  try {
    const licenseKey = request.query.license_key;
    const instanceName = request.query.instance_name;
    logger.log("Received request with licenseKey:", licenseKey, "and instanceName:", instanceName);

    const params = new URLSearchParams({license_key: licenseKey, instance_name: instanceName}).toString();
    logger.log("Constructed query parameters:", params);

    response.status(200).send("testActivateLicense completed successfully.");
  } catch (error) {
    logAndSendError(error);
  }
});
javascript firebase google-cloud-platform google-cloud-functions query-string
1个回答
0
投票

我会以这种方式获取网址参数:

new URLSearchParams(request.url.split("?")[1]).get("param");
© www.soinside.com 2019 - 2024. All rights reserved.