[英]Vuex and firebase: The user id is undefined in the firebase database
I am creating an e-commerce web site.我正在创建一个电子商务 web 网站。
Now I finished creating the new account with email and password.现在我完成了使用 email 和密码创建新帐户。
And I want to insert the user email, full name, and timestamp in the database.我想在数据库中插入用户 email、全名和时间戳。
As you can see in the picture below, I could see the USER data in the google chrome dev console.如下图所示,我可以在 google chrome 开发控制台中看到 USER 数据。
But when I checked the firebase database in the browser, I cannot see the user id.但是当我在浏览器中检查 firebase 数据库时,我看不到用户 ID。 And instead, I see undefined in the user id column.
相反,我在用户 ID 列中看到未定义。
Now I am on the step3 process.现在我正在进行第 3 步过程。
I cannot figure out why it's happening, so I hope you can help me out.我不知道为什么会这样,所以我希望你能帮助我。
This is my store/index.js file.这是我的 store/index.js 文件。
import fireApp from '@/plugins/firebase'
export const state = () => ({
user: null,
error: null,
busy: false,
jobDone: false
})
export const mutations = {
setUser (state, payload) {
state.user = payload
},
setError (state, payload) {
state.error = payload
},
clearError (state, payload) {
state.error = null
},
setBusy (state, payload) {
state.busy = payload
},
setJobDone (state, payload) {
state.jobDone = payload
},
}
export const actions = {
signUpUser({commit}, payload) {
commit('setBusy', true)
commit('clearError')
//1.Signup new user.
//2.Update firebase user profile & set local user data.
//3.Add user data into database
//4.Attach user to consumer group
let newUser = null
fireApp.auth().createUserWithEmailAndPassword(payload.email, payload.password)
.then(user => {
newUser = user
var user = fireApp.auth().currentUser;
user.updateProfile({ displayName: payload.fullname })
const currentUser = {
id: user.uid,
email: payload.email,
name: payload.fullname,
role: 'consumer'
}
console.log('USER', currentUser)
commit('setUser', currentUser)
})
.then(() => {
const userData = {
email: payload.email,
fullname: payload.fullname,
createdAt: new Date().toISOString()
}
fireApp.database().ref(`users/${newUser.uid}`).set(userData)
})
.then(() => {
commit('setJobDone', true)
commit('setBusy', false)
})
.catch(error => {
commit('setBusy', false)
commit('setError', error)
})
}
}
export const getters = {
user (state) {
return state.user
},
error (state) {
return state.error
},
busy (state) {
return state.busy
},
jobDone (state) {
return state.jobDone
}
}
This is because the promise returned by createUserWithEmailAndPassword()
method resolves with an UserCredential
object and not with a User
one.这是因为
createUserWithEmailAndPassword()
方法返回的 promise 使用UserCredential
object 解析,而不是使用User
one。
You should use the user
property of the UserCredential
, as follows:您应该使用
UserCredential
的user
属性,如下所示:
let newUser = null
fireApp.auth().createUserWithEmailAndPassword(payload.email, payload.password)
.then(userCredential => {
newUser = userCredential.user;
//...
Note also that you don't need to call fireApp.auth().currentUser
to get the user
.另请注意,您无需调用
fireApp.auth().currentUser
来获取user
。
When using the createUserWithEmailAndPassword()
method, on successful creation of the user account, this user will also be signed in to your application, so just get the user with userCredential.user
, as explained above.使用
createUserWithEmailAndPassword()
方法时,在成功创建用户帐户后,该用户也将登录到您的应用程序,因此只需使用userCredential.user
获取用户,如上所述。
In addition, note that the updateProfile()
method is asynchronous and returns a Promise, which you need to include in your promises chain.此外,请注意
updateProfile()
方法是异步的,并返回 Promise,您需要将其包含在您的 Promise 链中。
So the following should do the trick (untested):所以下面应该做的伎俩(未经测试):
signUpUser({commit}, payload) {
commit('setBusy', true)
commit('clearError')
//1.Signup new user.
//2.Update firebase user profile & set local user data.
//3.Add user data into database
//4.Attach user to consumer group
let user = null;
fireApp.auth().createUserWithEmailAndPassword(payload.email, payload.password)
.then(userCredential => {
user = userCredential.user;
return user.updateProfile({ displayName: payload.fullname });
})
.then(() => {
const currentUser = {
id: user.uid,
email: payload.email,
name: payload.fullname,
role: 'consumer'
}
console.log('USER', currentUser)
commit('setUser', currentUser)
const userData = {
email: payload.email,
fullname: payload.fullname,
createdAt: new Date().toISOString()
}
return fireApp.database().ref(`users/${user.uid}`).set(userData)
})
.then(() => {
commit('setJobDone', true)
commit('setBusy', false)
})
.catch(error => {
commit('setBusy', false)
commit('setError', error)
})
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.