Reputation: 772
I have a vue App with vuefire installed. Following the docs here: https://vuefire.vuejs.org/vuefire/getting-started.html#plugin, I have the main.js file :
import Vue from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'
import { firestorePlugin } from 'vuefire'
Vue.config.productionTip = false;
Vue.use(firestorePlugin);
new Vue({
router,
store,
render: h => h(App)
}).$mount('#app')
and the firebase.js file like this:
import firebase from "firebase";
const config = {
apiKey: "XXXXXX",
authDomain: "XXXXX",
databaseURL: "XXXXX",
projectId: "XXXXXXX",
storageBucket: "XXXXXX",
messagingSenderId: "XXXXXXX",
appId: "XXXXX"
};
firebase.initializeApp(config);
export const db = firebase.firestore();
And here is the home component
<template>
<div>
<button @click="signIn">Log in with google</button>
</div>
</template>
<script>
import firebase from "firebase";
import db from "@/firebase"
export default {
methods: {
signIn() {
const provider = new firebase.auth.GoogleAuthProvider();
firebase
.auth()
.signInWithPopup(provider)
.then(result => {
const malakas = {
userId: result.user.uid,
email: result.user.email,
displayName: result.user.displayName,
photoURL: result.user.photoURL
};
db.collection("malakes")
.doc(result.user.uid)
.set(spreadOparatorTest, { merge: true });
})
.catch(err => console.log(err));
}
}
};
</script>
<style lang="scss" scoped>
</style>
the weird thing is that in db.collection(...)
i get:
TypeError: Cannot read property 'collection' of undefined
because the db that i am importing gets imported as undefined. But if I change the db.collection(...)
to firebase.firestore().collection(...)
it works fine but i do not understand why.
Upvotes: 0
Views: 5719
Reputation: 3744
the problem is that you need to import a few dependencies separately... this is a good safe way:
import firebase from "firebase/app";
require('firebase/firestore')
require('firebase/auth')
const config = {
apiKey: "XXXXXX",
authDomain: "XXXXX",
databaseURL: "XXXXX",
projectId: "XXXXXXX",
storageBucket: "XXXXXX",
messagingSenderId: "XXXXXXX",
appId: "XXXXX"
};
firebase.initializeApp(config);
export const db = firebase.firestore();
export const auth = firebase.auth();
then your components can import em like this:
import firebase from 'firebase/app'
import { db, auth } from "./firebase" // <--- or wherever the config file is
export default {
methods: {
signIn() {
const provider = new firebase.auth.GoogleAuthProvider();
auth
.signInWithPopup(provider)
.then(result => {
const malakas = {
userId: result.user.uid,
email: result.user.email,
displayName: result.user.displayName,
photoURL: result.user.photoURL
};
db.collection("malakes")
.doc(result.user.uid)
.set(spreadOparatorTest, { merge: true });
})
.catch(err => console.log(err));
}
}
};
Hope this helps!
Upvotes: 3