0

I am coding with firebase. My problem is that the field is every time called at another number. So in a firestore document I have 6: value, or 23: value, ...
I am trying to get these fields like this:

 const data = doc.data();
 var number = 7;
 var el_already = data.[number];
        ...  

But it already says, that the data.[el] is an unexpected token. The code brokes. I hope you can help me.
~filip

Filip Degenhart
  • 494
  • 3
  • 15

2 Answers2

1

Your code is syntactically incorrect. Correct code:

 ...
 var number = 7;
 var el_already = data[number]; // Remove "." after  "= data"
 ...
UtkarshPramodGupta
  • 6,416
  • 6
  • 25
  • 49
-1

Inside javascript objects numbers can not appear as keys. So you need to convert it to string when saving data and when retrieving data.

let obj = {};
let num = 23;
obj[num.toString()] = "Twenty three";

var usersRef = ref.child("users");
usersRef.set(obj);

const data = doc.data();
var number = 23;
var el_already = data.[number.toString()];
Vishu Bhardwaj
  • 261
  • 3
  • 12