1
var seat;
var emp = db.collection('BookedTicketData').get().then((snapshot) => {
    snapshot.docs.forEach((doc) => {
        var data = doc.data()
        console.log(data.AllSeat);
        seat = data.AllSeat
    })
})

seat.forEach((val) => {
    for(i = 1; i<=37;i++){
        if(val == i){
            html += `
                <input type="checkbox" class="dn checkme" name="s-size" id="${j}"  value="${j}"/>
                <label for="${j}" class="col-2 br-pill f7 m-1 btn text-dark bg-white border-dark grow">${j}</label>
            `
        }
    }
})

document.getElementById('loader').innerHTML = html;

I have applied this code to get the value of seat from firebase and it is an array i am matching those value with a loop but i am getting undefined the value of seat outside that function.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Divyanshu Bhaskar
  • 335
  • 1
  • 4
  • 15

2 Answers2

1

You can try moving your logic inside then:

var emp = db.collection('BookedTicketData').get().then((snapshot) => {
  var seat;
  snapshot.docs.forEach((doc) => {
    var data = doc.data()
    console.log(data.AllSeat);
    seat = data.AllSeat
  })
  return seat;
}).then(seat => {
  seat.forEach((val) => {
    let html = ''
    for (i = 1; i <= 37; i++) {
      if (val == i) {
        html += `
                <input type="checkbox" class="dn checkme" name="s-size" id="${j}"  value="${j}"/>
                <label for="${j}" class="col-2 br-pill f7 m-1 btn text-dark bg-white border-dark grow">${j}</label>
            `
      }
    }
    document.getElementById('loader').innerHTML = html;
  })
})

Clarity
  • 10,730
  • 2
  • 25
  • 35
0

use async await syntax to resolve your promise. Actually what's happening in your code is seat.forEach is run before seat is assigned a value that's why it is undefined.Change your code like this...

var seat;
var emp = await  db.collection('BookedTicketData').get(); 

make sure to use await inside an async function, otherwise it won't work.

Again i have applied this code but now checkbox is not selectable

   var emp = db.collection('BookedTicketData').get().then((snapshot) => {
                                            snapshot.docs.forEach((doc) => {
                                                var data = doc.data()
                                                seat = data.AllSeat
                                            })
                                        }).then(() => {
                                            for (j = 1; j <= 37; j++) {

                                                html += `
                                                    <input type="checkbox" class="dn checkme" name="s-size" id="${j}"  value="${j}"/>
                                                    <label for="${j}" class="col-2 br-pill f7 m-1 btn text-dark bg-white border-dark grow">${j}</label>
                                                        `

                                        }


                                        document.getElementById('loader').innerHTML = html;


 emp.docs.forEach((doc) => {
                     var data = doc.data()
                     console.log(data.AllSeat);
                     seat = data.AllSeat
                   })


 this will work now

  seat.forEach((val) => {
                 for(i = 1; i<=37;i++){
                     if(val == i){
                               html += `
                          <input type="checkbox" class="dn checkme" name="s-size" id="${j}"  value="${j}"/>
               <label for="${j}" class="col-2 br-pill f7 m-1 btn text-dark bg-white border-dark grow">${j}</label>

                         `
                     }
                 }
       })
   document.getElementById('loader').innerHTML = html;
Divyanshu Bhaskar
  • 335
  • 1
  • 4
  • 15