【问题标题】:Accessing the content of events in Javascript?在 Javascript 中访问事件的内容?
【发布时间】:2017-11-07 11:14:29
【问题描述】:

我正在尝试打印出我的云功能所知道的事件。我在 Atom 控制台中尝试了 console.log(event),但我得到了这个错误:

Uncaught Error: Cannot find module 'firebase-functions'
at Module._resolveFilename (module.js:455:15)
at Module._resolveFilename 

我不熟悉 Javascript 或 Atom,所以我不知道为什么我没有得到预期的行为,我的猜测是我需要授权试图访问我在 Firebase 上的安全后端的脚本。

到目前为止,这是我的云功能:

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

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

admin.initializeApp(functions.config().firebase);

exports.sendPushNotification = functions.database.ref('/
iMessenger/{Messages}/{id}').onWrite(event => {

//I really want to see all that's inside of 'event'. How do I see this info??
console.log(event)
const payload = {
  notification: {
    title:'New message arrived',
    body:'Hello World',
    badge:'1',
    sound:'default',
    }
};
  return admin.database().ref('/iMessenger/{Messages}/{id}/toUserDisplayName').once('value').then(allToken => {
    if (allToken.val()){
      const token = Object.keys(allToken.val());
      return admin.messaging().sendToDevice(token, payload).then(response => {

   });
  };
 });
});

【问题讨论】:

    标签: node.js firebase firebase-realtime-database google-cloud-functions


    【解决方案1】:

    使用console.dir(event) 将列出event 中的所有可用方法和参数

    【讨论】:

    • 我试过了,但是得到了这个错误:module.js:457 Uncaught Error: Cannot find module 'firebase-functions'(...), @mast3rd3mon,你用什么软件来运行你的 javascript?我正在使用 Atom,它不像使用我很熟悉的 xCode。
    • 该错误与打印event的每个方法和属性无关。您遇到的错误与丢失的文件/模块有关。至于我用什么来运行代码,没关系
    • 我明白了。关于如何找到这个丢失的模块的任何建议?在已经通过终端并使用以下命令安装 googleCouldFunction 之后:npm install -g @google-cloud/functions-emulator 和 npm install -g firebase-tools。我希望自动生成的 javascript 文件中存在必要的模块,Abd firebase 甚至能够上传我的云功能,所以到目前为止一切似乎都正常。 @mast3rd3mon,还需要做什么才能获得这些模块?
    • 我之前没用过那个模块所以我不太确定
    【解决方案2】:

    Console.log(event) 显示在 Firebase 的云功能日志中。缺少模块是因为我的编辑器无法与我认为的 firebase 模块完全交互,因为 firebase 接受了我的功能并且通知正在通过;成功实施的证明。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-02-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-28
      • 2010-10-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多