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

248
Visualizações
Converting JSON to Array with Cloud Functions

When I use this function I get the following result from my realtime database. It looks like a json object. How can I turn that to an array or retrieve the string userName? snapshot.userName is not working.

const functions = require('firebase-functions');
const admin = require('firebase-admin');

admin.initializeApp();

   exports.Push = functions.database.ref('/placeID/{pushId}/')
   .onCreate((snapshot, context) => {
 
      console.log(snapshot)    
   })

enter image description here

enter image description here

about 4 years ago · Santiago Gelvez
2 Respostas
Responde à pergunta

0

A Realtime Database DataSnapshot object is a container that contains your data. To get the data it contains, you need to retrieve its value using the val() method:

console.log(snapshot.val());

To get the userName from your data, you can use either:

console.log(snapshot.val().userName)
// note: if "userName" is missing, this will log `undefined`

or

// more useful with large snapshots
console.log(snapshot.child("userName").val())
// note: if "userName" is missing, this will log `null`

See following link for more info about the DataSnapshot object and available methods/properties: https://firebase.google.com/docs/reference/functions/providers_database.datasnapshot

Added note: The DataSnapshot class overrides the toJSON() method, this is why when you log it, you saw the data it contained rather than the DataSnapshot's own methods/properties.

about 4 years ago · Santiago Gelvez Relatório

0

snapshot is a reference to a Firestore document. In order to get the data contained in a document doc you need to call doc.data(). So in your case it would be doc.data().userName.

See the documentation for some examples.

about 4 years ago · Santiago Gelvez 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