【问题标题】:Cloud Functions for Firebase ( No Log Messages)Firebase 的云功能(无日志消息)
【发布时间】:2017-07-01 20:12:33
【问题描述】:

这是我第一次使用 Cloud Functions for Firebase 和 Node.js,所以

我不知道为什么我在 Firebase 控制台上看不到任何日志。

至少,我已经成功部署了下面的功能

'use strict';

 const functions = require('firebase-functions');

 //import admin module
 const admin = require('firebase-admin');
 admin.initializeApp(functions.config().firebase);


 /* Listens for new messages added to events and sends a notification to 
 subscribed users */
 exports.pushNotification = 
  functions.database.ref('/events/{pushId}').onWrite( event => {

  console.log('Push notification event triggered');
  /* Grab the current value of what was written to the Realtime Database 
   */
    var valueObject = event.data.val();

     /* Create a notification and data payload. They contain the     
     notification information, and message to be sent respectively */ 
   const payload = {

     data: {
        eventType: valueObject.eventType,
        receiver: valueObject.receiver,
        requester: valueObject.requester
      }
     };

     /* Create an options object that contains the time to live for the 
      notification and the priority. */
      const options = {
          priority: "high",
          timeToLive: 60 * 60 * 24 //24 hours
      };

      return admin.messaging().sendToTopic(valueObject.receiver, payload, 
       options).then(function (response) {

            console.log("Successfully sent message:", response);
        })
        .catch(function (error) {
            console.log("Error sending message:", error);
        });
       });

但是,即使我将一些数据写入“事件”数据库,我也无法在 Firebase 控制台上看到任何日志消息。 这是否意味着这个功能根本没有被触发?

如果有人能提出这个问题的原因,我将不胜感激。

【问题讨论】:

  • 默认情况下,函数调用会记录在 Cloud Functions for Firebase 控制台中,即使您的代码中没有任何 console.log() 语句也是如此。这不是发生在你身上吗?
  • @FrankvanPuffelen 不,我看不到任何日志,但它说现在有 15 次处决。每当我再次部署该函数时,我都会额外执行 2 次。
  • 你能找到我的代码关于这个问题的任何问题吗?
  • 嗯...我不知道那里发生了什么。我总是看到登录console.firebase.google.com/u/0/project/_/functions/…

标签: android node.js firebase google-cloud-messaging firebase-cloud-messaging


【解决方案1】:

如果您已在控制台中正确设置了应用程序,那么在触发该函数时没有理由不进行日志记录。

这使您有可能没有启动该功能。一个可能的原因是数据没有写入函数中提供的路径。

那么,您是向/events 添加一个值,例如'true''ABC',还是向它添加一个子项,例如{'ABC' = true}? 在.ref(/events/{pushId})的情况下,函数将在后一种情况下被调用,而不是前者。

【讨论】:

  • 我添加了一个孩子作为后一种情况。这真的很奇怪,因为我的控制台显示每当我向数据库添加一个孩子时,执行次数就会增加。是否可以在没有任何日志消息的情况下执行函数?
  • 嗯,不,每次启动函数时都会创建一个默认日志。你的情况很奇特!让我以更彻底的方式浏览您的代码并进行更多研究。如果我有更新的答案,我会进行编辑。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-20
  • 1970-01-01
  • 2017-04-25
  • 2019-05-10
  • 1970-01-01
相关资源
最近更新 更多