【发布时间】:2020-07-22 05:21:08
【问题描述】:
使用 Firebase realtime 和 Vue.js 使用 Vuex 构建具有身份验证和用户配置文件的基本应用。 尝试通过 Vuex 商店按 ID 检索用户详细信息时,我收到这个讨厌的错误“fb.db.ref(...).then is not a function”。
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
})
},
商店(出现错误的地方)
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: {
}
})
Index.html with sn-p(虽然我不确定我是否需要这个)
<!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>
非常感谢任何提示! 谢谢。
【问题讨论】:
标签: javascript firebase vue.js firebase-realtime-database