我试图连接到firebase数据库并创建一个用于消息传递的类,但是由于某种原因,在初始化fireabase应用程序时,我得到了ReferenceError: self is not defined
。
import app from "firebase/app";
import "firebase/messaging";
const options = {
apiKey: process.env.FIREBASE_API_KEY,
authDomain: process.env.FIREBASE_AUTH_DOMAIN,
databaseURL: process.env.FIREBASE_DATABASE_URL,
projectId: process.env.FIREBASE_PROJECT_ID,
storageBucket: process.env.FIREBASE_STORAGE_BUCKET,
messagingSenderId: process.env.FIREBASE_MESSAGING_SENDER_ID
};
class Firebase {
constructor() {
app.initializeApp(options);
this.messaging = app.messaging();
}
}
const FirebaseApp = new Firebase();
export default FirebaseApp;
完整错误如下:
/node_modules/@firebase/messaging/dist/index.cjs.js:2110
if (self && 'ServiceWorkerGlobalScope' in self) {
^
ReferenceError: self is not defined
如果我从this.messaging=...
中删除了constructor()
,那么错误就消失了。