Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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);
  });
};
like image 390
Nathan Wailes Avatar asked Nov 01 '18 17:11

Nathan Wailes


People also ask

How do I enable Firebase logs?

To enable Analytics debug mode in your browser, install the Google Analytics Debugger Chrome extension. Once installed, enable the extension and refresh the page. From that point on, the extension will log events in your app in debug mode. You can view events logged in the DebugView in the Firebase console.

Does Firebase use Log4j?

December 21, 2021 Update: Firebase does not use Log4j 2 and is not impacted by the issues identified in CVE-2021-44228 and CVE-2021-45046.


2 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();.

like image 120
Renaud Tarnec Avatar answered Sep 21 '22 20:09

Renaud Tarnec


You can also use view logs as it said in the documentation:

https://firebase.google.com/docs/functions/writing-and-viewing-logs#viewing_logs

To view logs with the firebase tool

firebase functions:log

To view logs for a specific function

firebase functions:log --only <FUNCTION_NAME>
like image 29
Monireh Dashtiani Avatar answered Sep 25 '22 20:09

Monireh Dashtiani