hey, im getting some issues with my netlify functions. i’ve got a my own getUserData function for my login page, but i got some errors with it…
import fs from ‘fs’;
export default async (event, context) => {
const queryParams = event.queryStringParameters;
// if (!queryParams || !queryParams.number) {
// return new Response(JSON.stringify({ message: ‘Number parameter is required’ }), {
// status: 400,
// headers: {
// ‘Content-Type’: ‘application/json’
// }
// });
// }
// const { number } = queryParams;
const number = queryParams.number;
// Construct the file path based on the number parameter
const filePath = `/.netlify/functions/getUserData_${number}.json`;
try {
// Check if the file exists
fs.accessSync(filePath, fs.constants.F_OK);
// File exists, read its contents
const userData = fs.readFileSync(filePath, 'utf8');
return new Response(userData, {
headers: {
'Content-Type': 'application/json'
}
});
} catch (error) {
// File doesn't exist
return new Response(JSON.stringify({ message: 'User not found' }), {
status: 404,
headers: {
'Content-Type': 'application/json'
}
});
}
};
getUserData.mjs code ^^^
{“number”:“sleepy”,“ssn”:“050102-1377”,“name”:“Sleepy 1.8”,“expiry”:“2025-04-22”,“imageUrl”:“/assets/proFENI.png”}
getUserData_dak.json code ^^
login.js code
document.getElementById(“login-button”).addEventListener(“click”, function () {
const a = document.getElementById(“user-number-input”).value;
fetch(“/.netlify/functions/getUserData?number=” + a).then(a => a.json()).then(a => {
if (a && !a.msg) {
localStorage.setItem(“userData”, JSON.stringify(a));
window.location.href = “index.html”;
} else {
alert(“Fel kod!”);
}
}).catch(a => {
console.error(“Error:”, a);
alert(“An error occurred while trying to fetch user data.”);
});
});
document.addEventListener(“DOMContentLoaded”, function () {
document.getElementById(“no-button”).addEventListener(“click”, function () {
window.location.href = “https://discord.gg/”;
});
});
website https://663f5c659af016e412a06ff2--bk-idn.netlify.app/
error / issue
This function has crashed
An unhandled error in the function code triggered the following message:
TypeError - Cannot read properties of undefined (reading ‘number’)
Stack trace
TypeError: Cannot read properties of undefined (reading 'number')
at Module.getUserData_default (file:///var/task/getUserData.mjs:14:30)
at Runtime.handler (file:///var/task/___netlify-bootstrap.mjs:3734:33)
at Runtime.handleOnceStreaming (file:///var/runtime/index.mjs:1199:38)
in https://663f5c659af016e412a06ff2--bk-idn.netlify.app/.netlify/functions/getUserData?number=dak
Connection details
and before when i didnt have all those “//” i got the issue
{“message”: “Number parameter is required” }
in /.netlify/functions/getUserData?number=dak