Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

125
Visualizações
Nuxt firebase write doc after createUserWithEmailAndPassword fail

The idea of register form is

  1. add user email and password to firebase auth, this is successful, then
  2. add new document using their uid as document id and insert street field and value; to profile collection, failed without error.

The user and email are added to the auth users in firebase console, but the document writing still unsuccessfull with no error on the console either.

Been scratching my head for awhile.

Here's my code

<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 rule with no restriction

rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {
    match /{document=**} {
      allow read;
      allow write;
    }
  }
}
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Try like following:

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)
    }
  }
},
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda