GN Vageesh
GN Vageesh

Reputation: 370

Get array length from firebase firestore

Recently I started working on a project using Next JS and Firebase... I have added the data in the Cloud Firestore. I can get the details back from the firestore but not get the length of an array. Here is the image of the data structure in firestore enter image description here

I have tried this code

let len = 0
const ref = firebase.firestore().collection("users").doc(user.uid).get().then(snap => {
len = snap.data().disliked1.length
console.log(len)
})

But I get undefined

How do I get the length of this disliked1 array length?

Upvotes: 1

Views: 2369

Answers (1)

Dharmaraj
Dharmaraj

Reputation: 50850

I would make sure the ID is correct by logged it before and also if the snapshot received exists.

let len = 0
console.log(user.id)
const ref = firebase.firestore().collection("users").doc(user.uid).get().then(snap => {
  if (snap.exists) {
    console.log(snap.data())
    len = snap.data().disliked1.length
  } else {
    console.log("Snapshot not found")
  }
  console.log(len)
})

Upvotes: 1

Related Questions