Firebase functions not logging "console.log()" statements

Viewed 9034

I've been struggling with the function below not having its console.log() statements show up in the Firebase logs. If I take out everything starting with the db.collection call, the console.log() statements at the top will show up, but once I add that db.collection call, none of the console.log() statements show up in Firebase's logs.

I am not extremely familiar with JavaScript (I normally use Python for back-end programming), so this may be an issue with how Promises work. I'm looking into this now.

Any idea what's going on?

exports.purchaseItem = functions.https.onCall((data, context) => {
  console.log('data:')
  console.log(data)
  let lbcCustomerStripeToken = data.lbcCustomerStripeToken
  let lbcStoreId = data.lbcStoreId
  let amount = data.amount
  console.log('lbcCustomerStripeToken:')
  console.log(lbcCustomerStripeToken)
  console.log('lbcStoreId:')
  console.log(lbcStoreId)
  console.log('amount:')
  console.log(amount)

  let lbcFee = Math.round(amount * 0.02)

  db.collection('stores').doc(lbcStoreId).get().then(lbcStore => {
    if (!lbcStore.exists) {
      console.log('No such product!');
    } else {
      console.log('Document data:', product.data());
    }
    console.log('storeInfo:')
    console.log(lbcStore.data())
    return {message: 'Success'}
  })    
  .catch((error) => {
    console.log(error);
  });
};
3 Answers

You have to return the promise returned by the asynchronous get() method, see the doc here which indicates that "To return data after an asynchronous operation, return a promise. The data returned by the promise is sent back to the client."

So the following should work:

exports.purchaseItem = functions.https.onCall((data, context) => {
  console.log('data:')
  console.log(data)
  let lbcCustomerStripeToken = data.lbcCustomerStripeToken
  let lbcStoreId = data.lbcStoreId
  let amount = data.amount
  console.log('lbcCustomerStripeToken:')
  console.log(lbcCustomerStripeToken)
  console.log('lbcStoreId:')
  console.log(lbcStoreId)
  console.log('amount:')
  console.log(amount)

  let lbcFee = Math.round(amount * 0.02)

  return db.collection('stores').doc(lbcStoreId).get().then(lbcStore => {
    if (!lbcStore.exists) {
      console.log('No such product!');
    } else {
      console.log('Document data:', product.data());
    }
    console.log('storeInfo:')
    console.log(lbcStore.data())
    return {message: 'Success'}
  })    
  .catch((error) => {
    // To be adapted here, see https://firebase.google.com/docs/functions/callable#handle_errors
    console.log(error);
  });
};

Also, note that you should adapt your code for the error handling part, see https://firebase.google.com/docs/functions/callable#handle_errors.

Finally, be sure that your db constant is defined with admin.firestore();.

You can see the data that printed via console.log() in the specific function's Log tab in the Firebase console:

https://console.firebase.google.com/u/0/project/<project-name>/functions/logs

*Change <project-name> to yours (without < >).

*You may need to select specific function/log level to see the log in live.

Related