Mace
Mace

Reputation: 141

Get nested data from Firebase Firestore

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

Upvotes: 0

Views: 302

Answers (2)

Mace
Mace

Reputation: 141

Fixed by using optional chaining

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

Upvotes: 1

on9_jai
on9_jai

Reputation: 120

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

Upvotes: 1

Related Questions