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

160
Visualizações
What does setDoc function return?

I'm using firebase in my reactjs+typescript project. I want to write a document in firebase and I'm using setDoc to do this job.

import { doc, setDoc } from 'firebase/firestore';

setDoc(doc(database, `path`), objData)

When you check the function return it says Promise<...>. But, how am I supposed to use then() catch() promise handlers if I don't know what the promise returns when it fullfil/reject?

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

I'm not very familiar with Firebase but after looking around it seems the return is just going to be whether or not the setDoc() was successful. So the .then() would mean yes and the .catch() would mean no. So really all you would want is something like this

import { doc, setDoc } from 'firebase/firestore';

setDoc(doc(database, `path`), objData)
.then(() => {
console.log("Successful")})
.catch((error) => {
console.log(`Unsuccessful returned error ${error}`)});

Again I am not fully sure about the exact values returned as I could not find much but if there is a result value given you would place it in the .then((result) => {console.log(result)}).

about 4 years ago · Juan Pablo Isaza Relatório

0

The firebase documentation suggest using await, so here you have a complete example of error handling with await and cleanup with finally.

import { doc, setDoc } from 'firebase/firestore';


async function run() {
    try {
        await setDoc(doc(database, `path`), objData)
    } catch (e) {
        console.error(e); // handle your error here
    } finally {
        console.log('Cleanup here'); // cleanup, always executed
    }
}

run();
about 4 years ago · Juan Pablo Isaza Relatório

0

In Typescript, you don't need to specify the promise return type precisely. Here's an example of how to use promise handlers with setDoc:

import { doc, setDoc } from 'firebase/firestore';

setDoc(doc(database, `path`), objData)
  .then((data) => {console.log('data', data);})
  .catch((err) => {console.log('err', err);})
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