0

I was trying to get data from the notes array in the data object. I'm getting the object from mongodb and I want to get the array called notes. but I can't do it. for some reason. In java there is like Object.get(notes) and I can get a specific field within the object but I don't know how to do that in js. And I haven't been able to find something that works elsewhere online.

here is my code

axios.post('/api/user/notes')
.then(res => {
   console.log( Object.values(res.data));
   dataSet = Object.values(res.data); 
   console.log(dataSet[0]); 
   console.log(dataSet.notes[0]);      
   
}).catch(err => {
    console.log('it didnt work' + err); 
});

image

RashadArbab
  • 85
  • 1
  • 1
  • 9

1 Answers1

1

dataSet is an array. So, to get the notes, you have to first select the first element of data, dataSet[0] and then the notes array. To get the first note, it would be dataSet[0].notes[0]. Also, beware that you are setting a global variable (perhaps on a browser's window object) when you type dataSet = ... What you probably want is to declare a variable local to the function with var dataSet = ...

Keith
  • 984
  • 6
  • 9