寄存器形式的思想是
- 将用户电子邮件和密码添加到firebase auth,这是成功的,然后
- 使用其
uid
作为文档id添加新文档,并插入街道字段和值;到profile
集合,失败而无错误
用户和电子邮件被添加到firebase控制台中的auth用户中,但文档编写仍然没有成功,控制台上也没有错误。
有一段时间我一直在挠头。
这是我的代码
<template>
<v-form @submit.prevent="createUser">
<v-text-field
v-model="register.email"
label="Email"
required
/>
<v-text-field
v-model="register.password"
label="Password"
required
/>
<v-text-field
v-model="register.street"
required
label="Street"
/>
<v-btn
type="submit"
>
Register
</v-btn>
</v-form>
</template>
<script>
import Vue from 'vue'
import { createUserWithEmailAndPassword, onAuthStateChanged } from 'firebase/auth'
import { doc, setDoc } from 'firebase/firestore'
import { auth, db } from '~/plugins/firebase.js'
export default Vue.extend({
data () {
return {
register: {}
}
},
methods: {
async createUser () {
await createUserWithEmailAndPassword(
auth,
this.register.email,
this.register.password
).catch((error) => {
console.log(error.code)
console.log(error.message)
})
onAuthStateChanged(
auth,
(user) => {
if (user) {
const ref = doc(db, 'profile', user.uid)
const document = {
street: this.register.street
}
try {
setDoc(ref, document)
} catch (e) {
alert('Error!')
console.error(e.code)
console.error(e.message)
}
}
}
)
}
}
})
</script>
无限制的消防仓库规则
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
match /{document=**} {
allow read;
allow write;
}
}
}
尝试如下:
async createUser () {
await createUserWithEmailAndPassword(
auth,
this.register.email,
this.register.password
)
.then((user) => {
this.writeToFirestore(user)
})
.catch((error) => {
console.log(error.code)
console.log(error.message)
})
},
async writeToFirestore(user) {
if (user) {
const ref = doc(db, 'profile', user.id)
const document = {
street: this.register.street
}
try {
setDoc(ref, document)
} catch (e) {
alert('Error!')
console.error(e.code)
console.error(e.message)
}
}
},