I have created a module with FileId, Text input’s ., I want to make the FileId value dynamic code.
Can someone give me idea why I cannot get the FileId number as dynamic from my servercode. I want to get the value number from the div element from my html to my Server code so everytime a random ID’s it displays all the data base on that ID’s.
Here’s my serverside code:
const axios = require('axios');
exports.main = async (context, sendResponse) => {
const fileId = context.body.fileId; // Assuming the fileId is passed in the request body
if (!fileId) {
sendResponse({ body: 'File ID not provided in the request', statusCode: 400 });
return;
}
var config = {
method: 'get',
maxBodyLength: Infinity,
url: `https://api.hubapi.com/files/v3/files/${fileId}/signed-url?expirationSeconds=900`,
headers: {
Authorization: 'Bearer token-sample-here21321312312',
},
};
try {
const response = await axios(config);
const data = JSON.stringify(response.data);
sendResponse({ body: data, statusCode: 200 });
} catch (error) {
console.log(error);
sendResponse({ body: error.message, statusCode: 400 });
}
};
Here’s my client-sidecode:
<div class="file-wrapper">
<div id="fileIdNumber" class="file_id_val" data-file-id="{{ module.file_id }}">
{{ module.file_id }}
</div>
<a id="file_link_href" class="file_text_val" href="" target="_blank">
{{ module.file_text }}
</a>
<br>
<hr>
<div class="file-wrapper">
<div id="file-link"></div>
<span class="expires-at-label"></span>
</div>
</div>
<script>
var requestOptions = {
'method': 'GET',
'headers': {
'Content-Type': 'application/json',
}
};
async function loadFileDetails() {
try {
const fileIdElement = document.getElementById('fileIdNumber');
const fileId = fileIdElement ? fileIdElement.dataset.fileId : null;
if (!fileId) {
console.error('File ID not found in the HTML.');
return;
}
const expiresAt = Date.now() + 900 * 1000; // Add 60 seconds to the current timestamp
console.log(new Date(expiresAt));
const response = await fetch(
'/_hcms/api/createfilelink',
requestOptions
);
const formattedData = await response.json();
const fileLinkElement = document.getElementById('file-link');
const fileLinkElement2 = document.getElementById('file_link_href');
if (fileLinkElement && fileLinkElement2) {
fileLinkElement.textContent = formattedData.url;
fileLinkElement2.href = formattedData.url;
const expiresAtElement = document.createElement('span');
// Use options to specify the date and time format
const dateOptions = {
year: 'numeric',
month: 'short',
day: 'numeric',
hour: '2-digit',
minute: '2-digit',
second: '2-digit',
timeZoneName: 'short',
};
expiresAtElement.textContent = `${new Date(formattedData.expiresAt).toLocaleString(undefined, dateOptions)}`;
fileLinkElement.appendChild(expiresAtElement);
} else {
console.error('Element with id "file_link_href" not found.');
}
} catch (error) {
console.error('Error fetching signed URL:', error.message);
const fileLinkElement = document.getElementById('file-link');
if (fileLinkElement) {
fileLinkElement.textContent = 'Error: Failed to fetch signed URL.';
} else {
console.error('Element with id "file-link" not found.');
}
}
}
// Call the function to load file details
loadFileDetails();
</script>
This code is using module. What I want is if I insert this module more than 1 it keeps asking a fileId so if each Id is being inserted it auto detect and run the code and give each unique SIGNED URL