Reputation: 640
I'm trying to connect to my firestore using plain javascript. (I wanna get up to speed and running for now)
index.js:
import app from './firebase.js'
import { getFirestore } from 'https://www.gstatic.com/firebasejs/9.0.0/firebase-firestore.js'
const db = getFirestore(app)
However, this throws an error: Uncaught Error: Service firestore is not available
firebase.js:
import { initializeApp } from "https://www.gstatic.com/firebasejs/9.0.1/firebase-app.js";
const firebaseConfig = {
// configs
};
// Initialize Firebase
let app
export default app = initializeApp(firebaseConfig);
Then I import the script in my index.html
:
<!DOCTYPE html>
....
<script type="module" src="index.html"></script>
Note: I can read and write to the firestore using firebase web interface.
Upvotes: 6
Views: 10454
Reputation: 1
I've worked with React Native + expo project and had this issue. In my case I have error when error appears after using getFirestore from:
import { getFirestore, setLogLevel } from "firebase/firestore";
If you are having the same error, my suggestion is to use:
setLogLevel("debug");
and put your:
let db;
try {
db = getFirestore(app);
// console.log("Firestore instance:", db);
} catch (error) {
console.error("Error initializing Firestore:", error.message);
console.error("Error Stack:", error.stack);
console.error("Error Details:", error);
}
in this case you will see a lot more then just one line of error.
So, after checking what's going on, I realise that I have conflicting libraries installed. in app.json remove those:"
"@react-native-firebase/app", "@react-native-firebase/auth", "@react-native-firebase/crashlytics","
and uninstall @react-native-firebase from the project.
After those, my db become available and app again worked.
Upvotes: 0
Reputation: 1
In my case, I had my initial imports as:
import { initializeApp } from "https://www.gstatic.com/firebasejs/9.21.0/firebase-app.js";
import { getFirestore } from "https://www.gstatic.com/firebasejs/9.1.1/firebase-firestore.js";
import { getAnalytics } from "https://www.gstatic.com/firebasejs/9.21.0/firebase-analytics.js";
So I changed the version of my firestore from 9.1.1 to 9.21.0
Upvotes: 0
Reputation: 11
I had the same error which I corrected by using cdks with the same version. In my case I used the version (9.20.0)
"https://www.gstatic.com/firebasejs/9.20.0/firebase-app.js"
"https://www.gstatic.com/firebasejs/9.20.0/firebase-firestore.js"
Upvotes: 1
Reputation: 11
Change your import link
import { getFirestore } from "https://www.gstatic.com/firebasejs/9.0.0/firebase-app.js";
to
import { getFirestore } from "https://www.gstatic.com/firebasejs/9.0.1/firebase-app.js";
just update the firebase version from 9.0.0 to 9.0.1
Upvotes: 1
Reputation: 204
You need to upgrade your firestore version 9.0.0 to 9.4.0 and it work fine
import { getFirestore, collection, getDocs } from "https://www.gstatic.com/firebasejs/9.4.0/firebase-firestore.js";
Upvotes: 1
Reputation: 169
Using npm, but got the same error message. Restarted terminal & uninstalled and reinstall firebase, then worked...not sure which one that did it though.
Upvotes: 3
Reputation: 853
So if you want to use plain js (without bundlers like webpack), you would need to put your JS code into script
tag like so:
<script type="module">
import { initializeApp } from "https://www.gstatic.com/firebasejs/9.0.1/firebase-app.js";
const firebaseConfig = { ... };
const app = initializeApp(firebaseConfig);
</script>
Otherwise, if you want to use it like you intended to do so, you would need to:
Upvotes: 2