TypeError: firebase.messaging is not a function in node.js

前端 未结 5 1987
我在风中等你
我在风中等你 2021-01-17 19:58

While trying to fetch the FCM messages in node.js using firebase module, following error \"TypeError: firebase.messaging is not a function\" is occurring.

v         


        
相关标签:
5条回答
  • 2021-01-17 20:31

    The documentation for Firebase does not make it clear that there is a difference of features available based on the current environment. firebase.messaging is not available to a Node.js client, but is available from the firebase-admin package. However, this package alone comes with a different set of features specfically for firebase.messaging.

    You can see what's available to you based on your environment in the Firebase Reference docs. Specifically for your case the Node.js (Client) section.

    0 讨论(0)
  • 2021-01-17 20:31

    In my case was that I was importing firebase only as

    import '@firebase/app'
    

    instead of

    import * as firebase from '@firebase/app'
    

    after formatting it, I was able to access messaging from firebase.firebase.messaging()

    0 讨论(0)
  • 2021-01-17 20:34

    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 you https://github.com/firebase/quickstart-js/tree/master/messaging

    For nodeJS implementation, you have to use admin.messaging

    https://firebase.google.com/docs/reference/admin/node/admin.messaging

    // Get the Messaging service for the default app
    var defaultMessaging = admin.messaging();
    
    0 讨论(0)
  • 2021-01-17 20:35

    You must included import '@firebase/messaging' for it to work. So it's supposed to look like this:

    import * as firebase from 'firebase/app';
    import '@firebase/messaging';
    
    0 讨论(0)
  • 2021-01-17 20:49

    For react-native-firebase version 5.x.x and 6.x.x

    import firebase from '@react-native-firebase/app'
    import '@react-native-firebase/messaging'
    

    for example get token

    const fcmToken = await firebase.messaging().getToken();
    console.log(fcmToken)
    
    0 讨论(0)
提交回复
热议问题