Home > Blockchain >  Converting JSON to Array with Cloud Functions
Converting JSON to Array with Cloud Functions

Time:07-16

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

CodePudding user response:

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.

CodePudding user response:

Properties of objects in Javascript are case sensitive. Try this:

console.log(snapshot.userName);

New suggestion after receiving clarification:

console.log(snapshot.val());

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

  • Related