[英]TypeError: firebase.messaging is not a function in node.js
While trying to fetch the FCM messages in node.js using firebase module, following error "TypeError: firebase.messaging is not a function" is occurring.尝试使用 firebase 模块在 node.js 中获取 FCM 消息时,出现以下错误“TypeError: firebase.messaging is not a function”。
var firebase = require("firebase");
firebase.initializeApp({
apiKey: "xxxxxxx",
authDomain: "xxxxxxx",
databaseURL: "xxxxxxx",
projectId: "xxxxxxx",
storageBucket: "xxxxxxx",
messagingSenderId: "xxxxxxx"
});
const messaging = firebase.messaging();
How to rectify this error and how to use firebase module to pull messages??如何纠正这个错误以及如何使用 firebase 模块来拉取消息??
You must included import '@firebase/messaging'
for it to work.您必须包含
import '@firebase/messaging'
才能使其工作。 So it's supposed to look like this:所以它应该是这样的:
import * as firebase from 'firebase/app';
import '@firebase/messaging';
The criteria you are trying to use only works on the browser:您尝试使用的标准仅适用于浏览器:
You have to require firebase-messaging
, check this full sample it will guide youhttps://github.com/firebase/quickstart-js/tree/master/messaging您必须需要
firebase-messaging
,请查看此完整示例,它将指导您https://github.com/firebase/quickstart-js/tree/master/messaging
For nodeJS implementation, you have to use admin.messaging
对于 nodeJS 实现,你必须使用
admin.messaging
https://firebase.google.com/docs/reference/admin/node/admin.messaging https://firebase.google.com/docs/reference/admin/node/admin.messaging
// Get the Messaging service for the default app
var defaultMessaging = admin.messaging();
The documentation for Firebase does not make it clear that there is a difference of features available based on the current environment. Firebase 的文档没有明确说明基于当前环境的可用功能存在差异。
firebase.messaging
is not available to a Node.js client, but is available from the firebase-admin
package. firebase.messaging
不可用于 Node.js 客户端,但可从firebase-admin
包中获得。 However, this package alone comes with a different set of features specfically for firebase.messaging
.然而,仅此软件包中也有不同的功能集为specfically
firebase.messaging
。
You can see what's available to you based on your environment in the Firebase Reference docs.您可以在 Firebase 参考文档中根据您的环境查看可用的内容。 Specifically for your case the Node.js (Client) section.
专门针对您的情况Node.js (Client)部分。
react-native-firebase version 5.xx and 6.xx
react-native-firebase version 5.xx and 6.xx
import firebase from '@react-native-firebase/app'
import '@react-native-firebase/messaging'
for example get token
例如
get token
const fcmToken = await firebase.messaging().getToken();
console.log(fcmToken)
In my case was that I was importing firebase only as就我而言,我仅将 firebase 导入为
import '@firebase/app'
instead of而不是
import * as firebase from '@firebase/app'
after formatting it, I was able to access messaging from firebase.firebase.messaging()格式化后,我可以从 firebase.firebase.messaging() 访问消息传递
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.