Unable to utilize a variable for accessing an array element within a field

What is the reason I am unable to use a variable to access something inside my document? It seems that hard coding the field works, but when using a variable, it does not yield the expected result.

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

RI.findOne({$and: [{"building": building }, {"room": room}]}, (err, x) => {
    console.log(x.whiteBoard.votes[0]) //This line prints successfully
    console.log(x[item]) //However, this line returns undefined

Answer №1

This functionality is not clearly defined in any documentation, so it should not be expected to function properly. In order to retrieve the desired information, you must separate the accesses:

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

If you prefer to access keys using the X.Y notation, you can split the string based on the . and then access each part individually.

Another approach would be to utilize the get method from lodash: https://lodash.com/docs/4.17.4#get

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

How can I confirm that all elements have been properly reset to their original positions prior to making any further adjustments to them?

In need of some creative brainstorming, I'm working on a website design featuring ten overlapping cards. When the mouse hovers over a card, it expands while others move away. My goal is for each card to return to its original position once the cursor ...

The raycaster fails to detect collision with the .gltf model

When using the raycaster to detect mouse intersection with a cube, everything works perfectly: raycaster.setFromCamera(mouse, camera) const intersects = raycaster.intersectObject(cubeMesh) if(intersects.length > 0) console.log('intersecting ...

Issue encountered during compilation of cordova application

While using cordova CLI version 6.2.3 to build my app, I encountered an error in the console during the build process: ANDROID_HOME=/usr/lib/android-sdk JAVA_HOME=/usr/lib/jvm/java-8-openjdk-amd64 /usr/lib/jvm/java-8-openjdk-amd64/bin/java: symbol lookup ...

Adjust the div's height to 0

I need to dynamically set the height of a div element with a height of 34px to 0px. However, this div does not have a specific class or ID, so I can't use traditional DOM manipulation methods like .getElementById(), .getElementByClassName, or .getElem ...

Learn the steps to integrate Infinite Scrolling with Node.js, Angular.js, and Firebase!

UPDATE 8: CODE: <% include ../partials/header %> <script src="https://www.gstatic.com/firebasejs/3.5.2/firebase.js"></script> <script src="https://cdn.firebase.com/libs/firebase-util/0.2.5/firebase-util.min.js"></script> & ...

What could be causing my PDO query to not run when using fetch()?

I am attempting to execute an update statement using a button onclick event, but I am encountering difficulties when trying to run it with the fetch api. Here is the JavaScript function I have: const changeProductAvailability = (id,status) => { const ...

Leveraging JQuery to retrieve the string value from an onclick() event

Curious if there's a more efficient approach to tackle this issue, decided to seek input from the SO community... There's a third-party web page over which I have no control in terms of how it's displayed, but they do allow me to integrate ...

Retrieve weight measurements from a serial port by using Node JS

I'm a novice in node.js and I'm trying to retrieve weight data from a serial port. I have nodejs(v14.19.0) and npm(6.14.16) installed, and I'm attempting to obtain weight data using localhost:8080/get_weight. However, the script isn't f ...

Exploring the possibilities of integrating JSONP with Framework7

I've been attempting to retrieve images from the Instagram public API using ajax and JSONP: var target = https://www.instagram.com/p/BP3Wu_EDXsjdT5Llz13jFv2UeS0Vw0OTxrztmo0/?__a=1?callback=?'; $$.ajax({ ty ...

A guide to setting properties using a Proxy object

Within my class, I have included a Proxy which is structured as follows: export class Row<T extends ModelItems> { private _row: T = <T>{} public constructor(rowItems?: T) { if (rowItems) { this._row = rowItems } return new Proxy( ...

The ngOnChanges method fails to exhibit the anticipated modifications in a variable

Trying to grasp the concept of the ngOnChanges() callback, I created an example below. Despite having values for the attributes title and content in the Post interface during compile time, I do not see any logs from ngOnChanges. Please advise on the corre ...

The ng-click event in AngularJS does not function as expected when nested within another ng-repeat loop

I am facing an issue with ng-click in my Angular application (version 1.0.4). The first ng-click works fine, but the second one does not. <div class="menu-group" ng-repeat="module in modules"> <div ng-click="toggle($event, $parent)" ...

What are the steps to activate code suggestion with codemirror?

My goal is to implement CodeMirror for enabling users to input code in various languages like CSS, HTML, and JavaScript. One specific requirement is to provide hints to the user when typing code in CSS mode. div { padding- } The system should prompt th ...

Using the object value to map an array and populate the resulting elements

i have a function const [jobs, setJobs] = useState([]) const addJob = (title, role) => { const newJobs = [...jobs, { title, role}] setJobs(newJobs) } whenever a form is submitted, the addJob function creates state data containing ...

Confirming the information in two sections of a form

Could someone assist me with validating my form? Specifically, I need help ensuring that the house number field only accepts two numbers and the postcode field only allows 5 characters. I have implemented a pattern attribute for validation and I am curiou ...

What is the method for identifying which input field the user has chosen on a web page retrieved from a server?

I attempted the code below without achieving the desired outcome. Any assistance would be greatly appreciated. UIPasteboard *pb = [UIPasteboard generalPasteboard]; [pb setString:passwordName]; NSString *jScriptString; jScriptString = [NSString string ...

Ways to retrieve the pending count of XMLHttpRequests in JavaScript

I am trying to figure out how to capture the count of pending xmlhttprequests in a web page using javascript while working with selenium and python. It seems like there is a useful link that could help me get this information, but I'm having trouble ...

Learn how to retrieve information using the dash operator in ReactJS

I find it a bit amusing, but I'm encountering an issue. Can anyone lend me a hand with this? So, I'm using an API to fetch some data and it appears that the response from the API is in this format: start-time:2323232 end-time:2332323 Now, when I ...

Clicking anywhere outside a popup menu in JavaScript will deactivate a toggle and hide it

I have three different menu options: home,Clinic, and About. When I click on the Clinic option, a Megamenu appears, and if I click on it again, the Megamenu is hidden. I want the Megamenu to hide when clicking anywhere on the webpage. The issue is that cu ...

Tips for utilizing a ForEach loop in JavaScript to create an object with dynamically provided keys and values

Looking to create a JavaScript object with the following structure, where the Car Make and Model Names are provided from other variables. { "Sedan":{ "Jaguar":[ "XF", "XJ" ], "AUDI":[ "A6", ...