Создание базового c приложения с аутентификацией и профилями пользователей с Firebase в реальном времени и Vue. js с Vuex. Я получаю эту досадную ошибку «fb.db.ref (...). Then not function» при попытке получить сведения о пользователе по идентификатору через хранилище Vuex.
firebaseConfig. js
import firebase from 'firebase'
// firebase init goes here
var firebaseConfig = {
apiKey: "******",
authDomain: "******",
databaseURL: "******",
projectId: "******",
storageBucket: "******",
messagingSenderId: "******",
appId: "******"
};
firebase.initializeApp(firebaseConfig);
// firebase utils
const db = firebase.database()
const auth = firebase.auth()
console.log('inside config - auth:', auth)
console.log('inside config db:', db)
const currentUser = auth.currentUser
export {
db,
auth,
currentUser,
}
Функция для регистрации (аутентификация и запись пользователя в БД работает отлично)
signup() {
this.performingRequest = true
// first we authorize(separate to user object)
fb.auth.createUserWithEmailAndPassword(this.signupForm.email, this.signupForm.password).then(credential => {
// we send the user to vuex to set
this.$store.commit('setCurrentUser', credential.user)
// create user obj in firebase collection
fb.db.ref(`users/${credential.user.uid}`).set({
name: this.signupForm.name,
email: this.signupForm.email
}).then(() => {
this.$store.dispatch('fetchUserProfile')
this.performingRequest = false
this.$router.push('/dashboard')
}).catch(err => {
console.log('signup() - creating fb user object', err)
this.performingRequest = false
this.errorMsg = err.message
})
}).catch(err => {
console.log('signup() - creating fb auth object', err)
this.performingRequest = false
this.errorMsg = err.message
})
},
Store (где я получаю сообщение об ошибке)
import Vue from 'vue'
import Vuex from 'vuex'
const fb = require('../firebaseConfig.js')
Vue.use(Vuex)
export default new Vuex.Store({
state: {
currentUser: null,
userProfile: {}
},
mutations: {
setCurrentUser(state, val) {
state.currentUser = val
},
setUserProfile(state, val) {
state.userProfile = val
}
},
actions: {
fetchUserProfile({ commit, state }) {
fb.db.ref(`users/${state.currentUser.uid}`).then(res => { //ERROR FROM THIS LINE
commit('setUserProfile', res.data())
}).catch(err => {
console.log(err)
})
}
},
modules: {
}
})
Индекс. html с фрагментом (хотя не уверен, нужен ли он мне вообще)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width,initial-scale=1.0">
<link rel="icon" href="<%= BASE_URL %>favicon.ico">
<title><%= htmlWebpackPlugin.options.title %></title>
<script src="/__/firebase/7.13.2/firebase.js"></script>
</head>
<body>
<noscript>
<strong>We're sorry but <%= htmlWebpackPlugin.options.title %> doesn't work properly without JavaScript enabled. Please enable it to continue.</strong>
</noscript>
<div id="app"></div>
<!-- built files will be auto injected -->
</body>
</html>
Любые советы высоко ценится! Спасибо.