gpt4 book ai didi

javascript - fb.db.ref() 不是一个函数 - Firebase realtime with Vuejs

转载 作者:行者123 更新时间:2023-12-02 21:10:03 24 4
gpt4 key购买 nike

使用 Firebase realtime 和 Vue.js 以及 Vuex 构建具有身份验证和用户配置文件的基本应用。当尝试通过 Vuex 存储按 ID 检索用户详细信息时,我收到这个讨厌的错误“fb.db.ref(...).then 不是函数”。

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(尽管不确定我是否需要这个)

<!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>

非常感谢任何提示!!谢谢。

最佳答案

如果要读取数据,那么需要使用once():

  actions: {
fetchUserProfile({ commit, state }) {
fb.db.ref(`users/${state.currentUser.uid}`).once('value').then(res => {
commit('setUserProfile', res.val())
}).catch(err => {
console.log(err)
})
}
},

在此处查看更多信息:

https://firebase.google.com/docs/database/web/read-and-write#read_data_once

关于javascript - fb.db.ref() 不是一个函数 - Firebase realtime with Vuejs,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61123698/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com