javascript - <function name> error: FirebaseError: Firebase: No Firebase App '[DEFAULT]' has been
- c - Solaris 10 make Error code 1 Fatal Error when trying to build python 2.7.16 - Stack Overflow 推荐度:
- javascript - How to dismiss a phonegap notification programmatically - Stack Overflow 推荐度:
- javascript - Get the JSON objects that are not present in another array - Stack Overflow 推荐度:
- javascript - VS 2015 Angular 2 import modules cannot be resolved - Stack Overflow 推荐度:
- javascript - Type 'undefined' is not assignable to type 'menuItemProps[]' - Stack Overflow 推荐度:
- 相关推荐
I am building a web app using firebase local emulation. I have firebase authentication working but I'm getting errors that don't make sense when trying to query my database.
Here is the relevant testing code:
import { initializeApp, getApps } from 'firebase/app';
import { getAuth, connectAuthEmulator, signInWithEmailAndPassword, signOut } from 'firebase/auth';
import { executeQuery, getDataConnect, connectDataConnectEmulator } from 'firebase/data-connect';
import { getStudentByemail, connectorConfig } from '@firebasegen/default-connector';
let fbApp = null;
let dConnect = null;
let fbInitialized = false;
async function fetchFbConfig() {
try {
const response = await fetch('http://localhost:5001/<project-name>/us-central1/getApiKey');
if (!response.ok) {
throw new Error('Network response was not ok');
}
const fbConfig = await response.json();
return fbConfig;
} catch (error) {
console.error('Error fetching Firebase config:', error);
}
}
// Initialize Firebase with the fetched config
fetchFbConfig()
.then(fbConfig => {
if (!fbConfig) throw new Error('Failed to fetch Firebase config');
// Initialize Firebase only if it hasn't been initialized already
if (!getApps().length) {
fbApp = initializeApp(fbConfig);
const auth = getAuth(fbApp);
connectAuthEmulator(auth, "http://127.0.0.1:9099");
console.log('Initializing DataConnect...');
dConnect = getDataConnect(connectorConfig);
if (!dConnect) throw new Error('dataConnect not initialized');
connectDataConnectEmulator(dConnect, 'localhost', 9399);
dConnect.setInitialized();
fbInitialized = true;
window.GetStudentByemail = async function(_email) {
if (!fbInitialized || !dConnect) {
throw new Error('Firebase/DataConnect not initialized');
}
console.log('GetStudentByemail function activated with ', _email);
try {
const query = getStudentByemail({ email: _email });
console.log('Query:', query);
const qryRef = await executeQuery(dConnect, query);
return new Promise((resolve, reject) => {
qryRef.onSnapshot(
snapshot => resolve(snapshot.data()),
error => reject(error)
);
});
} catch (error) {
console.error('GetStudentByemail error:', error);
throw error;
}
};
window.signInUser = function(email, password, callback) {
if (!fbInitialized) {
console.error('Firebase not initialized');
callback(false);
return;
}
signInWithEmailAndPassword(auth, email, password)
.then((userCredential) => {
if (userCredential.user.emailVerified) {
console.log('User signed in:', userCredential.user);
callback(true);
} else {
console.error('Email not verified.');
signOut(auth);
callback(false);
}
})
.catch((error) => {
console.error('Error signing in:', error.code, error.message);
callback(false);
});
};
window.signInUser("<email>", "<password>", async function(isLoggedIn) {
if (isLoggedIn) {
try {
console.log('fetchStudentData function activated with ', "<email>");
const studentData = await window.GetStudentByemail("<email>");
console.log('Fetched student data:', studentData);
} catch (error) {
console.error('Error fetching student data:', error);
}
} else {
console.log('User not logged in.');
}
});
}
})
.catch((error) => {
console.error('Error initializing Firebase:', error);
});
The console log is:
Initializing DataConnect... index.js:35:18
User signed in: Object <> index.js:76:26
fetchStudentData function activated with <email> index.js:93:26
GetStudentByemail function activated with <email> index.js:48:20
GetStudentByemail error: FirebaseError: Firebase: No Firebase App '[DEFAULT]' has been created - call initializeApp() first (app/no-app).
The error is being thrown on the line
const query = getStudentByemail({ email: _email });
in the function GetStudentByemail. The query getStudentByemail give the correct response when called from within the data connection execution terminal in VSCode and is present and correct in the SDK.
Obviously the firebase app is created because the user is signed in and dataConnect is also correctly initialised, so the only thing that makes sense is if the execution of the promise preceeds the initialisation of the firebase app. I've read about promises being executed from a different queue (microtasks) that maybe causing this error, but I'm lost and clutching at straws and even if this is the case I have no idea how to solve.
Please excuse any incorrect terminology, I'm not a programmer so I'm very much out of my depth here.
Any help would be greatly appreciated.
I am building a web app using firebase local emulation. I have firebase authentication working but I'm getting errors that don't make sense when trying to query my database.
Here is the relevant testing code:
import { initializeApp, getApps } from 'firebase/app';
import { getAuth, connectAuthEmulator, signInWithEmailAndPassword, signOut } from 'firebase/auth';
import { executeQuery, getDataConnect, connectDataConnectEmulator } from 'firebase/data-connect';
import { getStudentByemail, connectorConfig } from '@firebasegen/default-connector';
let fbApp = null;
let dConnect = null;
let fbInitialized = false;
async function fetchFbConfig() {
try {
const response = await fetch('http://localhost:5001/<project-name>/us-central1/getApiKey');
if (!response.ok) {
throw new Error('Network response was not ok');
}
const fbConfig = await response.json();
return fbConfig;
} catch (error) {
console.error('Error fetching Firebase config:', error);
}
}
// Initialize Firebase with the fetched config
fetchFbConfig()
.then(fbConfig => {
if (!fbConfig) throw new Error('Failed to fetch Firebase config');
// Initialize Firebase only if it hasn't been initialized already
if (!getApps().length) {
fbApp = initializeApp(fbConfig);
const auth = getAuth(fbApp);
connectAuthEmulator(auth, "http://127.0.0.1:9099");
console.log('Initializing DataConnect...');
dConnect = getDataConnect(connectorConfig);
if (!dConnect) throw new Error('dataConnect not initialized');
connectDataConnectEmulator(dConnect, 'localhost', 9399);
dConnect.setInitialized();
fbInitialized = true;
window.GetStudentByemail = async function(_email) {
if (!fbInitialized || !dConnect) {
throw new Error('Firebase/DataConnect not initialized');
}
console.log('GetStudentByemail function activated with ', _email);
try {
const query = getStudentByemail({ email: _email });
console.log('Query:', query);
const qryRef = await executeQuery(dConnect, query);
return new Promise((resolve, reject) => {
qryRef.onSnapshot(
snapshot => resolve(snapshot.data()),
error => reject(error)
);
});
} catch (error) {
console.error('GetStudentByemail error:', error);
throw error;
}
};
window.signInUser = function(email, password, callback) {
if (!fbInitialized) {
console.error('Firebase not initialized');
callback(false);
return;
}
signInWithEmailAndPassword(auth, email, password)
.then((userCredential) => {
if (userCredential.user.emailVerified) {
console.log('User signed in:', userCredential.user);
callback(true);
} else {
console.error('Email not verified.');
signOut(auth);
callback(false);
}
})
.catch((error) => {
console.error('Error signing in:', error.code, error.message);
callback(false);
});
};
window.signInUser("<email>", "<password>", async function(isLoggedIn) {
if (isLoggedIn) {
try {
console.log('fetchStudentData function activated with ', "<email>");
const studentData = await window.GetStudentByemail("<email>");
console.log('Fetched student data:', studentData);
} catch (error) {
console.error('Error fetching student data:', error);
}
} else {
console.log('User not logged in.');
}
});
}
})
.catch((error) => {
console.error('Error initializing Firebase:', error);
});
The console log is:
Initializing DataConnect... index.js:35:18
User signed in: Object <> index.js:76:26
fetchStudentData function activated with <email> index.js:93:26
GetStudentByemail function activated with <email> index.js:48:20
GetStudentByemail error: FirebaseError: Firebase: No Firebase App '[DEFAULT]' has been created - call initializeApp() first (app/no-app).
The error is being thrown on the line
const query = getStudentByemail({ email: _email });
in the function GetStudentByemail. The query getStudentByemail give the correct response when called from within the data connection execution terminal in VSCode and is present and correct in the SDK.
Obviously the firebase app is created because the user is signed in and dataConnect is also correctly initialised, so the only thing that makes sense is if the execution of the promise preceeds the initialisation of the firebase app. I've read about promises being executed from a different queue (microtasks) that maybe causing this error, but I'm lost and clutching at straws and even if this is the case I have no idea how to solve.
Please excuse any incorrect terminology, I'm not a programmer so I'm very much out of my depth here.
Any help would be greatly appreciated.
Share Improve this question edited yesterday Doug Stevenson 316k36 gold badges453 silver badges472 bronze badges Recognized by Google Cloud Collective asked yesterday SJ_3289SJ_3289 211 silver badge3 bronze badges New contributor SJ_3289 is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.2 Answers
Reset to default 0I haven't used Data Connect before, but it seems to me that you should import getStudentByemailRef
instead of getStudentByemail
. The "ref" version is executed using executeQuery
, while the "non-ref" version ("action shortcut functions") according to the docs is just a function you call directly to execute the query. Or maybe you just want to skip executeQuery
and just invoke the action shortcut query directly instead of the ref. I'm making this guess based on what I see in the documentation. I suggest patterning your code based on what you see there.
I have managed to remove this error, I was passing the dataConnect instance to the wrong function, I should have been passing it to get getStudentByemailRef not executeQuery. There were other errors in my original code, the following code clears the No Firebase App Query and correctly queries my database, it may help anyone else who is working with Data Connect local emulation.
window.GetStudentUserByemail = async function(_email) {
if (!fbInitialized || !dConnect) {
throw new Error('Firebase/DataConnect not initialized');
}
try {
const query = getStudentByemailRef(dConnect, { "email" : _email });
const qryRef = await executeQuery(query);
return qryRef;
} catch (error) {
console.error('GetStudentUserByemail error:', error);
throw error;
}
};
Thanks Doug for your help.
- 都想取代PC 这些产品究竟还欠缺什么?
- Windows 10盗版不易?所以在中国普及速度极慢
- 苹果允许预装软件可卸载 或因遭受安卓手机阵型冲击
- 尊敬但不迷信 iPhone 6s这四点不如安卓机
- 移动互联网生态基石平台的吸引力分析
- 一晃三十年 回顾改变世界的那些Windows
- Intel毫不客气:ARM+Win8软硬件都不行
- python - tensorflow-gpu installation failed in colab - Stack Overflow
- python - Html in the inspect element is different that the one displayed on screen - Stack Overflow
- python - How to fix autocomplete menu flickering in PyCharm on Linux? - Stack Overflow
- node.js - Send GET request with pfx file using AXIOS failed - Stack Overflow
- r - Elegant vectorization of nested for loop - Stack Overflow
- testrigor - Unable to capture values from card display - Stack Overflow
- android - startActivity() on external application not bring it foreground in task of testing external application - Stack Overfl
- c++ - Which option has precendence if I enable and disable FrontEndHeapDebugOptions at the same time? - Stack Overflow
- GHC unable to find mingwinclude directory on Windows - Stack Overflow
- oop - How can I link an object to another object in Java? - Stack Overflow