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

130
Visualizações
How to clear and sanitize complex objects or any object in javascript

I'm trying to log every possible error to a firestore document, to achieve this, I have a function which receives the error object, and saves it to the firestore document... the thing is... that this error object can be anything, it can be a single string and not an object, or it could be a complex object, appearently with functions aswell, but firestore won't accept some data types, for example if the object has an undefined property firestore will not accept it...

So I'm trying to find a way to sanitize or clean an object from which I don't know what properties could have.

Right now i'm using this:

import {db} from 'src/firebase/config'
import { addDoc, collection } from 'firebase/firestore';
import {Platform} from 'quasar'

export const saveErrorLog = async (hotel, uid, ref, error) => {
  const guestRef = collection(db, 'error_logs');
  const cleanErrorObject = Object.entries(error).reduce((a,[k,v]) => (v ? (a[k]=v, a) : a), {})
  const logData = {
    platform: Platform.is || null,
    ref: ref || null,
    error: { ...cleanErrorObject },
    hotel: hotel || null,
    uid: uid || null
  }
  await addDoc( guestRef, logData )
}

This works most of the time, but sometimes firebase returns the same error of unsupported data types... I think it is because some error objects have functions aswell, but I'm not sure about it.

Thanks in advance.

about 4 years ago · Juan Pablo Isaza
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