1

I am writing cloud function in TypeScript, and trying to get Map object - AKA (nested objects, called maps) from firebase so I would be able to iterate through it.

This is the structure in my Firebase:

enter image description here

And I am trying to get the data like:

const tokenSettingsRef = db.collection('tokenSettings').doc('spread')
transaction.get(tokenSettingsRef).then((tokenSettingsDocSnapshot) => {


                            const tokenData = tokenSettingsDocSnapshot.data()
                            if (typeof tokenData !== 'undefined') {
                                console.log("tokennne3: " + tokenData.tokens[0])
                                console.log("tokennne4: " + tokenData)
                                console.log("tokennne5: " + tokenData.tokens)
                                console.log("tokennne1: " + tokenData.tokens.length())
                                console.log("tokennne2: " + tokenData.tokens.keys())
                                const variations = new Map(Object.entries(tokenData.tokens));
                                console.log("tokennne5: " + variations.keys)
                                console.log("tokennne6: " + variations.values)

None of the above doesn't give me Map so I could use it... Or log it out. I am getting data but all I can see is smtg like [object Object]

What am I missing here, I didn't have any issues with getting arrays or plain objects...


0

1 Answer 1

5

Based on the code in your question, I make the assumption that you read your data within a transaction.

The following should work:

var tokenSettingsRef = db.collection('tokenSettings').doc('spread');

db.runTransaction(transaction => {
    return transaction.get(tokenSettingsRef).then(tokenSettingsDocSnapshot => {

        if (!tokenSettingsDocSnapshot.exists) {
            throw "Document does not exist!";
        }

        var tokensMap = tokenSettingsDocSnapshot.data().tokens;
        //Let's print all the keys and values of the tokens map
        Object.keys(tokens).forEach(e =>
          console.log(`key=${e}  value=${tokens[e]}`)
        );
        //...... Continue the transaction
    });
}).then(function() {
    //....
})
.catch(error => {
    console.log('Transaction failed: ', error);
});

If you want to "get Map<number, number> object out of it", as asked in your comment below, you could do as follows:

var tokensMap = tokenSettingsDocSnapshot.data().tokens;

  const transformedTokensMap = new Map<number, number>();
  Object.keys(tokensMap).forEach(e => {
      transformedTokensMap.set(Number(e), tokensMap[e]);
});
Sign up to request clarification or add additional context in comments.

5 Comments

this works, thanks. How do I get Map object out of it, please?
Have a look at the specific section of the doc "Passing information out of transactions": firebase.google.com/docs/firestore/manage-data/…
I don't want to pass it outside of the transaction I want to use values inside the map for generating documents. I have a function that accepts Map<number, number> and I want to pass tokensMap to that function as a Map<number, number>.
do you have any advice on how to do that? Get Map<number, number> object out of it? Thanks
I did it this way, but I was hoping there is an easier way to do it... Thank you very very much...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.