#Undefined Array on Client Side
14 messages ยท Page 1 of 1 (latest)
On the client I wrote:
<button type="button" id="OpenAI_uploaded">List all files already uploaded to OpenAI</button>
<div id="uploaded_to_OpenAI_files_list"></div>
<script>
document.getElementById("OpenAI_uploaded").addEventListener("click", getOpenAIFiles);
function getOpenAIFiles(){
event.preventDefault();
fetch('http://localhost:3000/files_on_OpenAI', {
method: "GET"
})
.then(response => response.json())
.then(data => {
console.log(data);
document.getElementById("uploaded_to_OpenAI_files_list").innerHTML = response.data;
});
}
</script>```
On the server I wrote:
const { Configuration, OpenAIApi } = require("openai");
const configuration = new Configuration({
apiKey: apiKeys,
});
const openai = new OpenAIApi(configuration);
const response = await openai.listFiles();
let files = [];
for (let i = 0; i < response.data.data.length; i++) {
files.push({
filename: response.data.data[i].filename,
id: response.data.data[i].id
});
}
console.log(files);
res.send(files);
});```
Haven't fully read your server side code yet. How many results are expected in the array, does this differ in size?
For response.data.data.num.id, what is returned?
In this case I have 3 results expected. The number of results will depend on the number of files that have been uploaded to OpenAI so may be 0 or may be n.
Whatever the id is for that file, for example:
id: 'file-U4ZGERDvmkmModaqdhl9j0lf'
Get any further @hexed apex ? I have some time in ~2hrs if you're still picking at this
Thank you for checking in. I've spent the day working my day job ๐
I'm planning on looking at this later tonight after I do some work on a different project.
Mocking up some UX. Time consuming, sometimes tedious, but extremely helpful and clarifying before thinking architecture or laying down code.
We're in the same boat ๐ Finished at 6pm, scraped some wallpaper and then spent the past few hours writing and modifying sql querie
@hallow cove got it
Here is the new client script
document.getElementById("OpenAI_uploaded").addEventListener("click", getOpenAIFiles);
function getOpenAIFiles(){
event.preventDefault();
fetch('http://localhost:3000/files_on_OpenAI', {
method: "GET"
})
.then(response => response.json())
.then(data => {
let fileListHTML = '';
for (let i = 0; i < data.length; i++) {
const file = data[i];
fileListHTML += `<li>NAME: ${file.filename} || ID: ${file.id}</li>`;
}
document.getElementById("uploaded_to_OpenAI_files_list").innerHTML = fileListHTML;
});
}
</script>```
When you assign an array to innerHTML, the array is automatically converted to a string so the array returns "undefined". I had to iterate through the array.
I'm not super pleased with how the data is displayed, but good enough for me to move on for now.