Each then() should return a value or throw 1

Each then() should return a value or throw

outer.get('/account', function(req, res) {
  var id = req.user.uid
  var userRef = firebase.db.collection('users').doc(id)
  var profilePromise = userRef.get().then(doc => {
    if (doc.exists) {
      var profile = doc.data()
      profile.id = doc.id
      return profile // I assume you don't want to return undefined
//    ^^^^^^
    } else {
      throw new Error("Profile doesn't exist")
//    ^^^^^
    }
  })
  // More promises further on, which I wait for:
  // profilePromise.then(myProfile => { … });
})

Here is what the above code is Doing:
1. I’m using the firebase-admin SDK to get a reference to the user’s document in the users collection.
2. I’m using the get() method to get the document.
3. I’m using the then() method to wait for the document to be retrieved.
4. I’m using the doc.exists property to check if the document exists.
5. If the document exists, I’m returning the document data.
6. If the document doesn’t exist, I’m throwing an error.
7. I’m using the then() method to wait for the document data to be retrieved.
8. I’m using the myProfile variable to access the document data.

I hope this helps.

Similar Posts