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.
- 你知道多少?细数五花八门的生物识别技术
- 安卓4.4 vs iOS 7 谁是最佳移动操作系统
- c++ - Android OpenXR application java.io.FileNotFoundException: apexcom.meta.xrpriv-appVrDriverVrDriver.apk - Stack Overflow
- Youtube APi Fetching Gender Percentage of my channel - Stack Overflow
- apache spark - Can't save pyspark ML model :py4j.protocol.Py4JJavaError: An error occurred while calling o577.save. : ja
- for loop - CSS masonry grid with dynamic rows width - Stack Overflow
- caching - Pytorch: the cache is constantly increasing - Stack Overflow
- dart - Flutter local notification throws error when i try to setup scheduled Notification - Stack Overflow
- vb.net - Display image in picturebox at runtime - Stack Overflow
- How to import a svelte.ts component from another app where the import is with $lib - Stack Overflow
- mip sdk - Can you use the mip sdk to apply a mpip sensitivity label to a .json? - Stack Overflow
- swiftdata - Create state array from query results in SwiftUI view using results of a Swift data query - Stack Overflow
- Flutter - individual bloc states - Stack Overflow
- android - Unity Ads fail to initialize - Stack Overflow
- c# - After debugging, Visual Studio 2022 holding on to .NET Core program's .dll and I can't complete next build
- ms access - Execute VBScipt on local machine, triggered from remote desktop - Stack Overflow
- scipy - Problem with a simple script in which the Librosa Python library does not work well for me - Stack Overflow