I’m trying to pass some URL parameters to my cloud function and I keep facing an internal server error when I try to make a request. Does anyone have an idea of what might be causing it?
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);
}
}
Here is the logs from 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)
UPDATE
I have fixed the above error, now I am facing a new issue, where it seems that my query parameters are not being passed to the function correctly. This is from the GCP Log Explorer:
Received request with licenseKey: undefined and instanceName: undefined
Constructed query parameters: license_key=undefined&instance_name=undefined
Here is my latest testActivateLicense
for reference:
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);
}
});
UPDATE 2
I have fixed the above error by retrieving the license_key
and instance_name
with the following code instead:
const licenseKey = request.body.license_key;
const instanceName = request.body.instance_name;
UPDATE 3
Now I am attempting to actually validate the license using the lemonsqueezy validate
endpoint. Here is my current version of testActivateLicense
:
exports.testActivateLicense = onRequest(async (request, response) => {
try {
const licenseKey = request.body.license_key;
const instanceName = request.body.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);
logger.log("Making request to Lemonsqueezy API with URL:", validateUrl);
const res = await axios.post(activateUrl, params, {
headers: {
"Authorization": `Bearer ${testApiKey}`,
"Accept": "application/json",
"Content-Type": "application/vnd.api+json",
},
});
logger.log("Received response from Lemonsqueezy API with status:", res.status);
logger.log("Response data:", JSON.stringify(res.data, null, 2));
response.status(res.status).send(res.data);
} catch (error) {
logAndSendError(error);
}
});
And here are the logs I am getting:
Received request with licenseKey: ABCDEF-GHIJ-KLMN-OPQR-STUVWXYZ and instanceName: test_1234
Constructed query parameters: license_key=ABCDEF-GHIJ-KLMN-OPQR-STUVWXYZ&instance_name=test_1234
Making request to Lemonsqueezy API with URL: https://api.lemonsqueezy.com/v1/licenses/validate
Error: Error response data: {
"message": "The license key field is required. (and 1 more error)",
"errors": {
"license_key": [
"The license key field is required."
],
"instance_name": [
"The instance name field is required."
]
}
}
at entryFromArgs (/workspace/node_modules/firebase-functions/lib/logger/index.js:130:19)
at Object.error (/workspace/node_modules/firebase-functions/lib/logger/index.js:116:11)
at logAndSendError (/workspace/index.js:73:12)
at /workspace/index.js:104:5
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
Error: Error response status: 422
at entryFromArgs (/workspace/node_modules/firebase-functions/lib/logger/index.js:130:19)
at Object.error (/workspace/node_modules/firebase-functions/lib/logger/index.js:116:11)
at logAndSendError (/workspace/index.js:72:12)
at /workspace/index.js:104:5
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
TypeError: Cannot read properties of undefined (reading 'status')
at logAndSendError (/workspace/index.js:74:9)
at /workspace/index.js:104:5
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
2
Answers
UPDATE 4
To fix the issue in UPDATE 3, I have modified the way that the request parameters are retrieved. Here I am initially retrieving them from the query, and then they are retrieved from the body as well.
req.query
is used in express when you useurl/:param
regex so it will always beundefined
I can’t know how are those values passed so I cannot fix your code