I use angularfire2 for my Angular web apps and I recently upgraded to the v4.0.0-rc0. It behaves different in a few ways. The thing I need help with is using 'firebase' (the regular Firebase JS library) along side angularfire2 in the v4.0.0 version. In previous versions you would import:
import * as firebase from 'firebase';
and then use the regular Firebase JS library with statements like this:
firebase.database().ref().child("message").set("hi");
That worked fine in prior versions, see this video for more about that approach (it's a requirement when using storage):
https://www.youtube.com/watch?v=nMR_JPfL4qg#t=6m11s
However when I try to do the same thing with v4.0.0-rc0 I get this error message:
Firebase: No Firebase App '[DEFAULT]' has been created - call Firebase
App.initializeApp() (app/no-app).
I can read the message and realize that it thinks that initializeApp hasn't been called. But if I use the angularfire2 v4 code like this...
this.afDatabase.object("somepath").subscribe( (myData: any) => {
console.log("My data", data);
});
that is all fine, since I to the initializeApp in my app.module.ts. So the real issue is that previously the line:
AngularFireModule.initializeApp(environment.firebaseConfig),
worked for both angularfire2 AND the regular Firebase JS library, but now it doesn't. What is the right way to use the regular Firebase JS library for the database now? Do I call firebase.initializeApp in the app.module.ts too? That seems bad to make 2 initializeApp calls of course, but I don't know the right way to do it.
Figured out my issue, I needed to NOT give a custom name within my app.module.ts when I initialized the app. This works fine:
AngularFireModule.initializeApp(environment.firebaseConfig),
This was bad:
AngularFireModule.initializeApp(environment.firebaseConfig, 'myApp'),
you have to put in your import the new implementations modules and put
import * as firebase from 'firebase/app'; in the component where you are gonna use providersvar provider = new firebase.auth.GoogleAuthProvider();
import { AngularFireModule } from 'angularfire2';
import { AngularFireDatabaseModule } from 'angularfire2/database';
import { AngularFireAuthModule } from 'angularfire2/auth';
export const environment = {
production: false,
firebase: {
apiKey: "dfgdfgdsfgdfgsdfg",
authDomain: "fire-dfgdfg.sdfgdfg.com",
databaseURL: "https://fire-dsfgdfg.dfgdfgdf.com",
projectId: "fire-dfgdfg",
storageBucket: "fire-dfgdfg.appspot.com",
messagingSenderId: "65456456464654"
}
}
#NgModule({
imports: [
BrowserModule,
BrowserAnimationsModule,
AppRoutingModule,
BsDropdownModule.forRoot(),
TabsModule.forRoot(),
ChartsModule,
AngularFireModule.initializeApp(environment.firebase),
AngularFireDatabaseModule,
AngularFireAuthModule
],
declarations: [
AppComponent,
FullLayoutComponent,
SimpleLayoutComponent,
ChatLayoutComponent,
NAV_DROPDOWN_DIRECTIVES,
BreadcrumbsComponent,
SIDEBAR_TOGGLE_DIRECTIVES,
AsideToggleDirective
],
providers: [{
provide: LocationStrategy,
useClass: HashLocationStrategy
}],
bootstrap: [AppComponent]
})
Related
I struggle with the correct import of the firebase SDK. I use Vue3 and installed firebase via yarn add firebase
This is my firebase.js file:
import firebase from 'firebase/app';
However, this results in the following error: 1:1 error 'firebase/app' should be listed in the project's dependencies. Run 'npm i -S firebase/app' to add it import/no-extraneous-dependencies
import firebase from 'firebase';
This works, but I get the follwing warning:
It looks like you're using the development build of the Firebase JS SDK.
When deploying Firebase apps to production, it is advisable to only import
the individual SDK components you intend to use.
For the module builds, these are available in the following manner
(replace <PACKAGE> with the name of a component - i.e. auth, database, etc):
CommonJS Modules:
const firebase = require('firebase/app');
require('firebase/<PACKAGE>');
ES Modules:
import firebase from 'firebase/app';
So, first way seems to be recommended, but it does not work out for me. What am I doing wrong?
Update: This seems to be fixed in v2.23.4 (eslint-plugin-import).
Original answer: You're not doing anything wrong. This is a bug, probably related to the package definition in firebase, but it's discussed here inside the eslint rule's repo: https://github.com/benmosher/eslint-plugin-import/issues/2065
You either can use a comment to stop the error from occurring like so:
// eslint-disable-next-line import/no-extraneous-dependencies
import firebase from 'firebase/app';
Or you'll have to wait for the issue to be resolved. Downgrading to v2.22.1 of eslint-plugin-import might also work.
You need to create a config file for firebase and importing firebase here. Then, you
register the module you need and exported it so other file can use it as well.
import firebase from "firebase/app";
import "firebase/firestore";
import "firebase/auth";
import "firebase/storage";
const firebaseConfig = {
// you can get this from your firebase console.
apiKey: "",
authDomain: "",
projectId: "",
storageBucket: "",
messagingSenderId: "",
appId: "",
};
// init firebase
firebase.initializeApp(firebaseConfig);
// init services
const projectFirestore = firebase.firestore();
const projectAuth = firebase.auth();
const projectStorage = firebase.storage();
export { projectFirestore, projectAuth, projectStorage };
On your other file where you want to use your firebase, you could do something like this to import it.
import { projectAuth } from '../firebase/config'
const user = ref(projectAuth.currentUser)
Trying to use rollup with firebase ui and ending up with the following etc.
(!) Missing exports
https://rollupjs.org/guide/en/#error-name-is-not-exported-by-module
node_modules/firebaseui/dist/esm.js
auth is not exported by node_modules/firebase/app/dist/index.esm.js
rollup config:
import resolve from '#rollup/plugin-node-resolve';
export default [
{
input: 'Firebase.js',
output: {
file: 'Firebase.js',
format: 'es'
},
plugins: [
resolve()
]
}
Firebase.js:
import firebase from "firebase/app";
import "firebase/firestore";
import "firebase/auth";
import * as firebaseui from 'firebaseui'
firebase.initializeApp({...});
var ui = new firebaseui.auth.AuthUI(firebase.auth());
Also tried import firebaseui from 'firebaseui' but no luck.
Sans firebaseui, things work fine, e.g. can rollup a version accessing Firestore just fine.
first time posting here. Also quite new to VueJS, know some Angular.
I ran into an error when initializing Firebase(first time getting this error) in my VueJS project:
That showed up briefly after click event, so I had to slow down my network to take screenshot.
I've included <script src="https://www.gstatic.com/firebasejs/5.5.5/firebase.js"></script> in index.html
I also installed firebase(latest version) via npm with --save
And created 2 files: firebaseInit.js and firebaseConfig.js(which contains the api key, etc for web app):
In firebaseInit.js:
import firebase from 'firebase';
import 'firebase/firestore';
import firebaseConfig from './firebaseConfig';
const firebaseApp=
firebase.initializeApp(firebaseConfig);
export default firebaseApp.firestore();
and put:
export default { the config key value pairs obtained
from Firebase console }
in firebaseConfig.js.
Extra info(though I don't think this is the source of the problem):
I imported firebase with import firebase as 'firebase' in the vue file I used firebase auth() and createUserWithEmailandPassword() in the methods property, but alert() doesn't work.
import firebase from 'firebase';
export default {
name: 'register',
data: function() {
return {
email: '',
password: ''
};
},
methods: {
register: function(e) {
firebase
.auth()
.createUserWithEmailAndPassword(this.email, this.password)
.then(
user => {
alert(`Account made for ${user.email}`);
this.$router.push('/');
},
err => {
console.log(err.message);
}
);
e.preventDefault();
}
}
};
You created your firebaseInit.js but since you never import or require it, is it never executed, thus resulting in a "firebase not initialized" error when you try to use the firebase you imported from the generic firebase module instead.
Use export default firebaseApp in your firebaseInit.js (and not firestore as you do now) and then import firebase from 'path/to/firebaseInit.js' for example.
I have recently discovered the Firebase callable functions which allows me to call HTTPS trigger like function from the client side (and with auth() support).
I struggle to implement this new feature in my already existing Firebase web-client application.
I have some cloud functions running, among them are some HTTPS functions I would like to transform into an HTTPS callable function (with functions.https.onCall).
The documentation indicates:
Set up your client development environment
<script src="https://www.gstatic.com/firebasejs/4.12.0/firebase.js"></script>
<script src="https://www.gstatic.com/firebasejs/4.12.0/firebase-functions.js"></script>
And my code is:
import firebase from 'firebase';
import 'firebase/firestore';
const firebaseApp = firebase.initializeApp({
apiKey: '....',
authDomain: '....',
databaseURL: '....',
projectId: '....',
storageBucket: '....',
messagingSenderId: '....',
});
const db = firebaseApp.firestore();
const auth = firebaseApp.auth();
const functions = firebaseApp.functions();
export { db, auth, functions };
When I run my app, I got the following error:
Uncaught TypeError: firebaseApp.functions is not a function
I have tried yarn add firebase-functions and then import 'firebase-functions but then the app requires firebase-admin. I am affraid it is too much for a client-app so I might go in the wrong direction.
Can someone help me with this issue?
(!) This issue is NOT about the server-side Firebase SDK for Cloud Functions (Node JS). It is about calling Cloud functions directly from a Firebase web app.
Thank you!
UPDATE:
Thanks to #Andrew's post, this solves my issue:
My configuration
import firebase from 'firebase';
import 'firebase/firestore';
import '#firebase/functions';
import firestoreConfig from '#/config/firestore';
const firebaseApp = firebase.initializeApp(firestoreConfig /* The JSON configuration from my Firebase project */);
const db = firebaseApp.firestore();
const auth = firebaseApp.auth();
const functions = firebaseApp.functions();
export { db, auth, functions };
Using the configuration:
import { db, functions } from '#/database/firestoreInit';
export default {
addMessage(text) {
const addMessage = functions.httpsCallable('addMessage');
return addMessage({ text }).then(result => result);
},
}
I just ran into this same problem myself and solved it by installing and importing the #firebase/functions npm package. I found the solution on github here:
https://github.com/firebase/firebase-js-sdk/blob/master/packages/functions/README.md
From the README on github:
ES Modules
import firebase from '#firebase/app';
import '#firebase/functions'
// Do stuff w/ `firebase` and `firebase.functions`
CommonJS Modules
const firebase = require('#firebase/app').default;
require('#firebase/functions');
// Do stuff with `firebase` and `firebase.functions`
Hope that helps! The actual documentation is not very clear about having to do this in order to call the functions.
About #firebase/functions:
This package is not intended for direct usage, and should only be used via the officially supported firebase package.
This worked for me:
import * as firebase from 'firebase/app'; // Typescript
// import firebase from 'firebase/app'; // JS
import 'firebase/functions';
const myCallableFunc = firebase.functions().httpsCallable('myCallableFunc');
I don't know about importing firebase-functions with a CDN but if you're using npm then you don't need the firebase-functions package, just installing firebase will do.
Follow the steps mentioned here. Firebase cloud functions
I think there is nothing like firebaseApp.functions.
When using aurelia-cli, in a newly made project, I'm trying to include firebase in my bundle using the following code:
{
"name": "firebase",
"path": "../node_modules/firebase",
"main":"firebase",
"exports": "firebase"
}
Based on their documentation, this should make firebase globally available in my app (similar to $for jQuery).
What's causing this not to work?
In your main.js, try the following:
import firebase from 'firebase';
export function configure(aurelia) {
...
firebase.initializeApp({
apiKey: 'your_api_key',
authDomain: 'your_auth_domain',
databaseURL: 'your_database_url',
storageBucket: 'your_storage_bucket'
});
aurelia.start().then(() => aurelia.setRoot());
}
In app.js:
// Import firebase if the project was created using the Aurelia-CLI
// If you're using the Aurelia Esnext-Skeleton, you don't have to import firebase
import firebase from 'firebase';
export class App {
constructor() {
this.some_ref = firebase.database().ref('some_path');
}
}