gpt4 book ai didi

javascript - createUserWithEmailAndPassword 失败后 Nuxt firebase 写文档

转载 作者:行者123 更新时间:2023-12-04 14:48:01 27 4
gpt4 key购买 nike

注册表的思路是

  1. 将用户邮箱和密码添加到firebase auth,这样就成功了,然后
  2. 使用 uid 作为文档 ID 添加新文档,并插入街道字段和值;到 profile 集合,失败且没有错误。

在firebase console中添加了用户和email到auth users,但是还是写文档不成功,控制台也没有报错。

一直摸不着头脑。

这是我的代码

<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)
}
}
},

关于javascript - createUserWithEmailAndPassword 失败后 Nuxt firebase 写文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69645641/

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