ReferenceError: self 在设置 Firebase 时未定义

Posted

技术标签:

【中文标题】ReferenceError: self 在设置 Firebase 时未定义【英文标题】:ReferenceError: self is not defined on setup Firebase 【发布时间】:2021-05-11 20:38:03 【问题描述】:

我尝试设置 Firebase 以在 Web [Javascript] 上使用云消息传递,但我收到如下错误:

I20210208-15:21:52.756(7)? Exception while invoking method '/microfis_loanRequest/update' ReferenceError: self is not defined
I20210208-15:21:52.758(7)?     at isSupported (/Users/rabbit/Desktop/microfis/node_modules/@firebase/messaging/dist/index.cjs.js:1711:9)
I20210208-15:21:52.758(7)?     at Component.factoryMethod [as instanceFactory] (/Users/rabbit/Desktop/microfis/node_modules/@firebase/messaging/dist/index.cjs.js:1694:10)
I20210208-15:21:52.758(7)?     at Provider.getOrInitializeService (/Users/rabbit/Desktop/microfis/node_modules/@firebase/component/dist/index.cjs.js:222:39)
I20210208-15:21:52.759(7)?     at Provider.getImmediate (/Users/rabbit/Desktop/microfis/node_modules/@firebase/component/dist/index.cjs.js:120:33)
I20210208-15:21:52.759(7)?     at FirebaseAppImpl._getService (/Users/rabbit/Desktop/microfis/node_modules/@firebase/app/dist/index.node.cjs.js:230:49)
I20210208-15:21:52.760(7)?     at FirebaseAppImpl.firebaseAppImpl.(anonymous function) [as messaging] (/Users/rabbit/Desktop/microfis/node_modules/@firebase/app/dist/index.node.cjs.js:449:39)
I20210208-15:21:52.761(7)?     at Object.serviceNamespace [as messaging] (/Users/rabbit/Desktop/microfis/node_modules/@firebase/app/dist/index.node.cjs.js:429:45)
I20210208-15:21:52.761(7)?     at Object.<anonymous> (microfis/server/collection_hooks/mobile-app-hook/loan-request.js:53:30)
I20210208-15:21:52.762(7)?     at packages/matb33_collection-hooks.js:486:20
I20210208-15:21:52.762(7)?     at Array.forEach (native)

这里我做了什么设置

import firebase from 'firebase/app';
import 'firebase/messaging';

var firebaseConfig = 
    apiKey: "API_KEY",
    authDomain: "AUTH_DOMAIN.firebaseapp.com",
    projectId: "PROJECT_ID",
    storageBucket: "STORAGE_BUCKET.appspot.com",
    messagingSenderId: "SENDER_ID",
    appId: "APP_ID",
    measurementId: "MEASUREMENT_ID"
;
// Initialize Firebase
firebase.initializeApp(firebaseConfig);

const messaging = firebase.messaging();

我在这里尝试使用它:

    messaging.getToken( vapidKey: 'VAPID_KEY' ).then((currentToken) => 
        if (currentToken) 
            // Send the token to your server and update the UI if necessary
            // ...
            print(currentToken);
         else 
            // Show permission request UI
            console.log('No registration token available. Request permission to generate one.');
            // ...
        
    ).catch((err) => 
        console.log('An error occurred while retrieving token. ', err);
        // ...
    );

对此错误有任何想法,在此先感谢

【问题讨论】:

【参考方案1】:

确保您导入 Firebase SDK 脚本并在 index.html 中进行初始化。指令here.

【讨论】:

以上是关于ReferenceError: self 在设置 Firebase 时未定义的主要内容,如果未能解决你的问题,请参考以下文章

Next.js 纱线构建因 plotly.js 失败(发生构建错误 ReferenceError: self is not defined)

在 React-Native/Expo 移动应用程序上设置 Detox 时出错:“ReferenceError: element is not defined”

在 testRunner 设置为 jest-circus 的情况下访问 jasmine 会导致:ReferenceError: jasmine is not defined

ReferenceError:当我使用 vuejs 设置 PayPal Checkout 时,未定义 paypal

Bootstrap 设置错误:未捕获 ReferenceError:未定义 jQuery 和未捕获错误:Bootstrap 的 JavaScript 需要 jQuery

一旦我设置 nodeIntegration:false -> "Uncaught ReferenceError: require is not defined at Object.ur