0
votes

I'm trying to get some information from the update of a firestore file through functions.

I've already managed to get the firestore trigger right (functions is working when my firestore file is updated), but I cannot get the information that was updated through functions.

Here is my functions code:

import * as functions from "firebase-functions";
//const functions = require('firebase-functions');
const admin = require("firebase-admin");
admin.initializeApp();

exports.enviaMensagem = functions.firestore
  .document("Events/{eventID}/Listas/Convidados")
  .onWrite(async (change, context) => {
    const eventID = context.params.eventID;
    console.log("eventID: ", eventID);
    if (change.after.data()) {
        const data = change.after.data();

        if ( data!.reference) {
            console.log(data!.reference);
        } else {
            console.log('not possible to get the user reference');
        }

        console.log(data!.toString());
        console.log(change.after.toString());

    }
  });

[SOLVED] My problem number 1 is when I try console.log(data!.toString()); the return to the console becames [object Object] and not the actual data.

[SOLVED] My problem number 2 is that I cannot get the info that has been inserted to the firestore file.

this is the structure of the info that has been inserted to the firestore file: updated data structure

the structure is: {[reference key]:{apelido:'abc', estado:'def', referencia:[reference key]}}

I need to retrive the reference key, which is the primary tag for this map, and also inside the structure by the tag "referencia"

How can I retrive this reference key so I can use it?

Thanks for your help!

UPDATE Here is the print of my logged object: Logged object I need to retrive this long string that appears on top and botton of the log "bJaETeYN4hYJqRN9PnmWfrhs7eC2" This string is different for every user and I need the info of which user is doing the action.

UPDATE 2 I've managed to get what I need with the function Object.keys(data!)[0] I'm leaving the solution here so other can use it!

1

1 Answers

0
votes

If you want to log an object with JavaScript, don't call toString() on it. Just pass it directly:

console.log(data!)

The document you've written is a little bit awkward. For some reason, it contains a single field whose name is "bJaE...7eC2". Since you didn't show the code that writes this data, we can't really tell why this is, or if it's just a mistake.

If you need to work with the document exactly as written, you're going to have to refer to the referencia field through the oddly-named field it's in:

console.log(data!.bJaE...7eC2.referencia)

You will have to replace "bJaE...7eC2" with the full string you see in the document field.

But that field name is probably a mistake, and you should go back and fix your client code to do what you actually want.