createUserWithEmailAndPassword 失败后 Nuxt firebase 写文档
Nuxt firebase write doc after createUserWithEmailAndPassword fail
注册表的思路是
- 将用户邮箱和密码添加到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>
无限制的 firestore 规则
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)
}
}
},
注册表的思路是
- 将用户邮箱和密码添加到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>
无限制的 firestore 规则
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)
}
}
},