1

for some reason Firebase is returning promise instead of value

let customer123 = db.collection('CustomerDatabase').get().then((snapshot) => {
    snapshot.forEach((doc) => {
        if(businessNameValue === doc.data().BusinessName){
            let customerID = doc.data().CustomerID;
            console.log(customerID) // this is fine, i am able to ge the value.
            return customerID
        }
    })
})

console.log(customer123) //but this is giving me promise
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
RoniDoni
  • 47
  • 4
  • 2
    Your return statement is returning a value from the function passed to `forEach`, not the one passed to `then`. Nothing is being returned from the function passed to `then`. But `customer123` will still always be a promise, no matter what. `then` always returns a promise. – Doug Stevenson Sep 21 '21 at 05:34
  • 1
    As Doug said, then will always return a promise. It's not firebase that works like that, it's javascript that does. What you can do is use the await construct, and it will resolve the whole promise before assigning its value to customer123, but you need to be inside an async function – Gregorio Palamà Sep 21 '21 at 05:45
  • understood thank you so much, I'm still pretty new to this, havent got a chance to look up await/resolve/promise/and async functions yet, I will defiantly look up now. thank you – RoniDoni Sep 21 '21 at 15:37

0 Answers0