0

why can I not use a variable to access something inside my document? if I hard code the field it works but using a variable it does not.

building = "AS"
room = "243"
item = "whiteBoard.votes[0]"

RI.findOne({$and: [{"building": building }, {"room": room}]}, (err, x) => {
    console.log(x.whiteBoard.votes[0]) //works
    console.log(x[item]) //undefined
tom smith
  • 13
  • 1

1 Answers1

2

This functionality isn't defined anywhere- so it isn't expected to work. You have to split the accesses:

console.log(x["whiteBoard"]["votes"][0])

Alternatively if you need to be able to access keys using the X.Y notation, you can split the string on the . and access these individually.

Another option would be to use lodash's get method: https://lodash.com/docs/4.17.4#get

Derek Brown
  • 4,232
  • 4
  • 27
  • 44