Unable to connect React Native app to Firestore emulator - reactjs

I've tried just about EVERY solution on Stack Overflow and beyond, and still getting errors.
I've setup and confirmed the emulator runs fine. However, trying to connect the react native app to it returns the error:
Could not reach Cloud Firestore backend. Connection failed 1 times...
followed by a bunch of these warnings:
Connnection Webchannel transport errored .
Here's my config info
import * as firebase from 'firebase';
import { initializeApp } from "firebase/app";
import firestore from 'firebase/firestore';
const firebaseConfig = {
apiKey: "ZZZZ",
authDomain: "ZZZZ",
projectId: "ZZZZ",
storageBucket: "ZZZZ",
messagingSenderId: "ZZZZ",
appId: "ZZZ",
measurementId: "ZZZ"
};
firebase.initializeApp(firebaseConfig);
firebase.firestore().useEmulator("localhost", 8080);
I even replaced localhost with 0.0.0.0, same error.
Someone recommended changing the last line to
firebase.firestore().settings({
host: "localhost:8080",
ssl: false
});
which I tried, but still got the same errors.
Please what's the correct, VERIFIED way of doing this? I'd really appreciate it 'cos I want to spend hours on this!

I’m afraid you are not correctly importing firestore and neither initializing the emulator in the appropriate way. You are importing the Firebase function correctly, but there are a few things you need to change.
I will reference Firebase version 9 since it is the one that should be used, if you would like to still use version 8, I will leave the references where you can choose which version to use.
Instrument your app to talk to the emulators
Set up your in-app configuration or test classes to interact with Cloud Firestore as follows.
import { `getFirestore`, connectFirestoreEmulator } from "firebase/firestore";
// firebaseApps previously initialized using initializeApp()
const db = getFirestore();
connectFirestoreEmulator(db, 'localhost', 8080);
When you initialize Firebase you need to do it this way after installing firebase using npm.
const app = initializeApp(firebaseConfig);
and...
Initialize Cloud Firestore
Initialize an instance of Cloud Firestore:
import { initializeApp } from "firebase/app"
import { getFirestore } from "firebase/firestore"
const firebaseApp = initializeApp({
apiKey: '### FIREBASE API KEY ###',
authDomain: '### FIREBASE AUTH DOMAIN ###',
projectId: '### CLOUD FIRESTORE PROJECT ID ###'
});
const db = getFirestore();
At the end your file should look like this:
import { initializeApp } from "firebase/app";
import { getFirestore } from 'firebase/firestore';
import { getFirestore, connectFirestoreEmulator } from "firebase/firestore";
const firebaseConfig = {
apiKey: "ZZZZ",
authDomain: "ZZZZ",
projectId: "ZZZZ",
storageBucket: "ZZZZ",
messagingSenderId: "ZZZZ",
appId: "ZZZ",
measurementId: "ZZZ"
};
const app = initializeApp(firebaseConfig);
const db = getFirestore();
connectFirestoreEmulator(db, 'localhost', 8080);

Related

firestore' does not exist on type 'typeof import

Difficulty in setting up this app as firebase, exported from firebase/app doesn't seem to have the firestore() method on it (See attached image). This is my code. Someone please help me fix it.
// Import the functions you need from the SDKs you need
import firebase from 'firebase/app';
// TODO: Add SDKs for Firebase products that you want to use
// https://firebase.google.com/docs/web/setup#available-libraries
// Your web app's Firebase configuration
const firebaseConfig = {
apiKey: "-------",
authDomain: "--------",
projectId: "------------",
storageBucket: "------------",
messagingSenderId: "---------",
appId: "----------"
};
// Initialize Firebase
const app = firebase.initializeApp(firebaseConfig)
export const firestore = firebase.firestore()
Maybe you're using Firestore v9. So you might be using the methods from the old API
You can check what version of firebase you've installed by looking at the major version of the npm package in your package.json
Here is a snippet from the Firestore docs on how to initialize firestore with with the web version v9 (https://firebase.google.com/docs/firestore/quickstart)
// Initialize Cloud Firestore through Firebase
import { initializeApp } from "firebase/app"
import { getFirestore } from "firebase/firestore"
const firebaseApp = initializeApp({
apiKey: '### FIREBASE API KEY ###',
authDomain: '### FIREBASE AUTH DOMAIN ###',
projectId: '### CLOUD FIRESTORE PROJECT ID ###'
});
const db = getFirestore();

firebase_compat_app__WEBPACK_IMPORTED_MODULE_0__.default.database is not a function

I'm getting the error below when trying to connect my app with Firebase:
firebase_compat_app__WEBPACK_IMPORTED_MODULE_0__.default.database is not a function
I also did a test and I see that it happening only when connecting the database. If I comment on the database line, it works.
I also tried changing the rules on Firebase to true, and still not working.
Note: I'm trying to use realtime database
Here is my code:
import firebase from 'firebase/compat/app';
import 'firebase/compat/auth';
import 'firebase/compat/firestore';
const firebaseConfig = {
apiKey: process.env.REACT_APP_API_KEY ,
authDomain: process.env.REACT_APP_AUTH_DOMAIN ,
databaseURL: process.env.REACT_APP_DATABASE_URL,
projectId: process.env.REACT_APP_PROJECT_ID,
storageBucket: process.env.REACT_APP_STORAGE_BUCKET,
messagingSenderId: process.env.REACT_APP_MESSAGING_SENDER_ID,
appId: process.env.REACT_APP_APP_ID
};
firebase.initializeApp(firebaseConfig);
const auth = firebase.auth();
const database = firebase.database();
export{ firebase, auth, database}
Firebase has two databases:
Firestore
Realtime Database
While they're both part of Firebase, they're completely separate.
You're importing firebase/compat/firestore, so Firestore. But then you call firebase.database(), which is the Realtime Database.
You'll need to pick one and stick with it:
If you want to use Firestore, keep your import as is and access it with firebase.firestore().
If you want to use Realtime Database, keep the call as is but import firebase/compat/database.

Reactjs backend connection issue

My problem is that my browser is showing that fire store is not able to connect to the backend:
#firebase/firestore: Firestore (9.1.1): Could not reach Cloud Firestore backend. Connection failed 1 times. Most recent error: FirebaseError: [code=permission-denied]: Permission denied on resource project MY_PROJECT_ID.
This typically indicates that your device does not have a healthy Internet connection at the moment. The client will operate in offline mode until it is able to successfully connect to the backend.
My firebase.js code is
import firebase from "firebase/compat/app";
import "firebase/compat/auth";
import "firebase/compat/firestore";
import "firebase/compat/storage";
const firebaseConfig = {
apiKey: MY_API_KEY,
authDomain: MY_AUTH_DOMAIN,
projectId: MY_PROJECT_ID,
storageBucket: MY_STORAGE_BUCKET_URL,
messagingSenderId: MY_MESSAGING_SENDER_ID,
appId: MY_APP_IP,
measurementId: MY_MEASUREMENT_ID,
};
const firebaseApp = firebase.initializeApp(firebaseConfig);
const db = firebaseApp.firestore();
const auth = firebase.auth();
const provider = new firebase.auth.GoogleAuthProvider();
const storage = firebase.storage();
export { auth, provider, storage };
export default db;
Please help me .
New firebase v(9.1.1) is modular based version. Therefore you have to import everything as named import from respective files.
eg:-
import { initializeApp } from 'firebase/app'
import { getFirestore } from 'firebase/firestore'
const firebase = initializeApp({
apiKey: MY_API_KEY,
authDomain: MY_AUTH_DOMAIN,
projectId: MY_PROJECT_ID,
storageBucket: MY_STORAGE_BUCKET_URL,
messagingSenderId: MY_MESSAGING_SENDER_ID,
appId: MY_APP_IP,
measurementId: MY_MEASUREMENT_ID,
})
const firestore = getFirestore()
The method/function way of using firebase is not supported in v(9.1.1), this probably helps with tree shaking(loading only required files/functions, rather than entire library), which saves loading time and creates fast user experience.
For detailed information checkout these videos:-enter link description here
Also check firebase docs:-enter link description here
Can you please try to generate a new firebaseConfig file? You can do that by
Going to your project settings.
At the bottom of the page there's a section called Your apps.
On the top right of that section click the button labeled Add App.
Choose the icon for webapp (3rd from the left).
After choosing a name for the new webapp, you'll get your new fbconfig.
Copy new fbconfig to your project and try again.
Let me know what happens please.
EDIT 0 -

Is there something I am missing which means I can only write to firestore locally and not in production with Zeit Now deployed app

I am able to write to my firestore db locally without trouble but after I have deployed my next.js app the writing and updating functions dont result in any change on the DB.
I am initialising firebase like so
import 'firebase/firestore'
import 'firebase/auth';
const firebaseConfig = {
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,
appId: process.env.FIREBASE_APP_ID,
measurementId: process.env.FIREBASE_MEASUREMENT_ID
};
if (!firebase.apps.length) {
firebase.initializeApp(firebaseConfig);
}
const auth = firebase.auth();
export {
auth,
firebase
};
and my firestore rules are like this:
service cloud.firestore {
match /databases/{database}/documents {
match /{document=**} {
allow read, write, update: if true;
}
}
}
I can't find any errors (is there an easy way to see ZEIT now console.log outputs on the server side, right now I am just passing them back in the response).
Any small response would be much appreciated.

firebase.initializeApp is not a function

I'm trying to make a site in React, and I have added the Firebase library using npm i firebase --save
I am then trying to call this in my app, but I am not having much joy:
import * as firebase from "firebase";
const instance = firebase.initializeApp({
apiKey: '123',
authDomain: 'https://123.firebaseapp.com/',
databaseURL: 'https://123.firebaseio.com/'
});
export default instance;
No matter what I try, it throws:
firebase.initializeApp is not a function
I've checked its in my node_modules and i've reinstalled it, but I still have no such luck.
Any suggestions?
I had a the same problem with firestore in my Vue application.
I eventually came right by doing the following in my firebase.js file:
import { firebase } from '#firebase/app'
As opposed to :
import * as firebase from 'firebase/app'
Also if you are using any specific firebase modules you need to import them individually like so:
import '#firebase/auth'
import '#firebase/firestore'
const db = firebase.firestore()
const auth = firebase.auth()
export { db, auth }
Hope this helps somebody.
This is how I solved this
import firebase from "firebase";
const firebaseConfig = {
apiKey: "Api Key",
authDomain: "Domain",
databaseURL: "URL",
projectId: "ID",
};
const firebaseApp = firebase.initializeApp(firebaseConfig);
const db = firebaseApp.firestore();
export default db;
Can you please try with:
var firebase = require('firebase/app');
Credits.
Been having a similar issue with firebase v4.12.1 throwing error of initializeApp not being a function. I solved it by switching to using #firebase/app 0.1.10 npm package instead.
import { firebase } from '#firebase/app';
Using it in a next.js v5.1.0 project.
been having the same problem but in VUE, in case it works i solved importing each element separately
// This import loads the firebase namespace.
import firebase from 'firebase/app';
// These imports load individual services into the firebase namespace.
import 'firebase/auth';
import 'firebase/database';
import 'firebase/firestore';
import 'firebase/storage';
Hope can help ;)
This is the new firebase SKD modular recommendation. Avoid using the
import firebase from 'firebase/app';
and try to use modular import, such as:
import {initializeApp} from "firebase/app";
const instance = {
apiKey: '123',
authDomain: 'https://123.firebaseapp.com/',
databaseURL: 'https://123.firebaseio.com/'
};
export const app = initializeApp(instance);
references
https://firebase.google.com/docs/web/learn-more#modular-version
const auth = firebase.auth()
export { db, auth }`import * as firebase from "firebase";
require("#firebase/firestore");
// For Firebase JS SDK v7.20.0 and later, measurementId is optional
const firebaseConfig = {
apiKey: "",
authDomain: "",
projectId: "",
storageBucket: "",
messagingSenderId: "",
appId: "",
measurementId: ""
};
// Initialize Firebase
firebase.initializeApp(firebaseConfig);
export default firebase.firestore();

Resources