Home > Blockchain >  Get nested data from Firebase Firestore
Get nested data from Firebase Firestore

Time:10-10

Here is how I get the data:

enter image description here

Currently when I console.log(snapshotData.data); I get an array of this:

enter image description here

what I want to get is editorState, blocks, 0, text. I tried to get it using snapshotData.data.editorState.blocks.[0].text, and snapshotData.data.editorState.blocks.0.text and it didn't work

again, I want to get this:

enter image description here

On Firestore it's says: editorState is map, blocks -> array, 0 -> map, text -> string

enter image description here

CodePudding user response:

Try

// if blocks is an Array
snapshotData.data.editorState.blocks[0].text

or

// if '0' is a key corresponding to the smaller object
snapshotData.data.editorState.blocks["0"].text

Update

According to the screenshot uploaded, blocks is actually an array containing one single object.

You should use the first code block I provided, i.e.

snapshotData.data.editorState.blocks[0].text

CodePudding user response:

Fixed by using optional chaining

  console.log(snapshotData?.data?.editorState?.blocks[0]?.text);
  • Related