Problem to get last entry in a array in js

Hello Community,

I'm working on a dashboard app for my company and for that I want to get the status of a Project out of my mongo db database. the query works fine and for updating my list view I use some lines of Javascript in a js query. The problem is that I'm not able to access the last entry in my array of status. I tried it with pop(), slice(-1), [array.length - 1]
It seems that it is not possible to use a math operator, I'm super confused.
Can anybody help me?

Thanks in advance,

Flo

const project_data = query1.data.project_status;

let indexOf = project_data.length; 
let idx = indexOf - 1;
let statusData = project_data[idx]


projectstatus.setValue(statusData.status)

const project_data = query1.data.project_status.pop();

projectstatus.setValue(project_data.status)
const project_data = query1.data.project_status.slice(-1);

projectstatus.setValue(project_data.status)

this is the data from my query:

Hi @flo2204,

A couple things to try here - for starters, setValue returns a promise, so try

await projectstatus.setValue(statusData.status)

If that doesn't fix it, confirm if you can/can't access the data, try adding console.log(statusData.status) before any of the setValue lines, and check the debug console to see if the expected value is there. Then you can narrow it down the part that might be causing the issue.

Debug console can be accessed in the bottom right of the browser window.
image

Hey Mike,

thanks for the fast reply!
ok thanks I'm totally new to JS so I changed this. But the problem is that I cannot access the last entry of my array. When I look into the variables there is the wrong or no data:





it gives me no or just the first entry. When I manually put in idx = 6 it works, but not idx = 7 - 1 it seems that it's not possible to calculate something with the math operator. but also pop() and slice(-1) does not give me the last entry.

Thanks again for helping me,

Flo

Hi @flo2204,

What if you try this?

const statusValue = query1.data.projectStatus.slice(-1)
console.log( {statusValue} )
await projectStatus.setValue(statusValue)

You could put that whole query1.data.projectStatus.slice(-1) in the last line, but this allows you to try logging the result to the console.

Hey Mike,

Thanks for your help. I tried it out, and the console looks linke this:

image

so it is not the last entry in my list. Do you have any idear what can cause the problem?

Thanks,

Flo

Hi @flo2204,

I'm confused, that looks like the last entry based on your screenshot, no?

image