最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Firebase functions not logging "console.log()" statements - Stack Overflow

programmeradmin7浏览0评论

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);
  });
};

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);
  });
};
Share Improve this question edited Mar 17, 2023 at 16:25 Renaud Tarnec 83.1k10 gold badges97 silver badges129 bronze badges Recognized by Google Cloud Collective asked Nov 1, 2018 at 17:11 Nathan WailesNathan Wailes 12.2k10 gold badges75 silver badges114 bronze badges 1
  • 1 You may need to use the stackdriver library. – Phix Commented Nov 1, 2018 at 17:29
Add a ment  | 

3 Answers 3

Reset to default 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./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./docs/functions/callable#handle_errors.

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

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

https://firebase.google./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>

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./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.

发布评论

评论列表(0)

  1. 暂无评论