17

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);
  });
};
1
  • 1
    You may need to use the stackdriver library. Commented Nov 1, 2018 at 17:29

3 Answers 3

9

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

Sign up to request clarification or add additional context in comments.

Comments

4

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>

Comments

1

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.

6 Comments

The logs are not working and not shown in the logs of firebase platform
@KamelMili What do you see on the screen? What is the URL you’re trying access to?
Maybe you didn't pressed on the play icon on the right? it should show right away.
no, I pressed the play button, but it's always that way and the thing that I'm pretty mad about is the beta versions of firestore and firebase emulators. that I couldn't install due to java existence error and i do have java >_<
PS it worked the console but you should select function and info as type to see it
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.