-1

I have this JS Object that contains 3 "thread" objects each with different values. I am trying to get the values from each of the threads. I am able to print the entire json object, but I can't print individual values from them?

My code: console.log(threadArray) returns this in console: (which is perfect) enter image description here

but when I try to replace "threadArray" with "threadArray[0].threads[0]["_id" to get the first thread's ID, it says it Cannot read unknown value.

Does anybody know why?

Harley Swift
  • 168
  • 6
  • please provide a [mcve] – Daniel A. White Mar 09 '22 at 00:00
  • I can't really as I am extracting the JSON data from a Supabase Database, am I accessing the JSON object correctly by using [0].threads.. etc? – Harley Swift Mar 09 '22 at 00:01
  • See that little blue _i_ next to your array, hover over it. To get a real idea of what your array looks like at the time of logging, use `console.log("threadArray", JSON.stringify(threadArray)`. I'd also advise avoiding premature debugging; why are you even trying to log your values at that time? – Phil Mar 09 '22 at 00:15
  • How are you making the request? How are you trying to get the result? – Daniel A. White Mar 09 '22 at 00:35

1 Answers1

-1

It's because the object called "threads" is the same as "threadArray[0]" because it IS the first element in the array. You are calling the same object twice. Instead of that log, try using this one:

console.log(threadArray[0]._id);

I hope this helps! I will stay tunned so that you can tell me if it worked

Nick
  • 69
  • 1
  • 6
  • `._id` and `["_id"]` are exactly the same – Phil Mar 09 '22 at 00:14
  • Sorry, I didn't know that O - O. But I think I found the problem – Nick Mar 09 '22 at 01:24
  • Nope, the problem is that OP is trying to access properties of the object that are defined at a later time – Phil Mar 09 '22 at 01:25
  • Yes, you are right. But we don't have many details about the moment he defines the variables. Although, if he types "threadArray[0]._id" in the console, it will return the value correctly (Only if the array is a global variable). We should ask him for further details. – Nick Mar 09 '22 at 01:38