0

I use the Firebase console on my laptop to run this function and it works perfectly. I deploy to Firebase and trigger it but it gives me an 'Unhandled rejection' error. Any recommendations would be greatly appreciated. Thanks!!

exports.updateItems = functions.database.ref('/update').onUpdate(event => {
  return superagent
    .get(zohoItemsRef + 'items?' + zohoToken + zohoOrg)
    .query({
      authtoken: zohoToken,
      organization_id: zohoOrg
    })
    .set('cache-control', 'no-cache')
    .then(res => {
      res.body.items.forEach(function(item) {
        if (item.status === 'active') {
          var itemData = {
            name: item.name,
            price: item.rate,
            category: item.cf_product_category,
            partNumber: item.part_number
          }
          if (item.cf_quick_pick) {
            itemData.quickMenu = item.cf_quick_pick
          }
          itemRef.doc(item.item_id).set(itemData)
        }
      })
    })
    .catch(e => {
      console.log(e)
    })
})
Rory
  • 336
  • 1
  • 4
  • 12
  • 1
    FYI you can make a code block by selecting all the code and using the button with the curly braces to indent every line by 4 characters. – Doug Stevenson Feb 23 '18 at 00:09
  • 1
    Certainly there's more in the error message? Also, every `then` should have a matching `catch` to trap errors that happen earlier in the chain. Best practice is to log your error object there. – Doug Stevenson Feb 23 '18 at 00:10
  • 1
    Lastly, you have a `return` inside a forEach function. That's probably not what you intended. You can't return inside the forEach function and expect that to get returned from the then function. – Doug Stevenson Feb 23 '18 at 00:12
  • Thanks, @DougStevenson. I removed the return and placed a catch immediately after the `then` statement. – Rory Feb 23 '18 at 16:46
  • I found [this here](https://stackoverflow.com/a/46736847/1576574) so I'm going to play around with it. – Rory Feb 23 '18 at 16:48
  • My other errer was: `Error: Deadline Exceeded at /user_code/node_modules/firebase-admin/node_modules/grpc/src/node/src/client.js:554:15` I just assumed it was because of the rejection error. – Rory Feb 23 '18 at 16:49

0 Answers0