"Encountering a hiccup in executing map.reduce

var mapFunction1 = function() {

    for (var i = 0; i < this.money.length; i++) {
        emit("balance", this.money[i].amount);
    }

};

var reduceFunction1 = function(keyBalance, valuesBalance) {
    return Array.sum(valuesBalance);
};

db.users.mapReduce(mapFunction1, reduceFunction1, {
    out : "balance"
})

db.balance.find()

However, an error has occurred

uncaught exception: map reduce failed:{ "errmsg" : "exception: TypeError: Cannot read property 'length' of undefined\n at _funcs1 (_funcs1:2:37) near 'is.money.length; i++) ' (line 2)", "code" : 16722, "ok" : 0

Answer №1

This situation indicates that the property "length" is undefined on an object within your document, most likely because the property does not exist.

When running JavaScript evaluations in a mapReduce process, it is important to be thorough and "strict" in testing for these conditions:

var mapFunction1 = function() {
   if ( this.hasOwnProperty("account") {
       for (var idx = 0; idx < this.account.length; idx++) {
           emit("saldo", this.account[idx].balance);
       }
   }
}

Using .hasOwnProperty() is essential to prevent such errors by verifying the existence of the object.

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

JQuery - Triggering $(document).ready() before the elements are fully loaded

While working on retrieving data from a MVC3 action that returns Json with parameters and content as a string, I encountered an issue. After appending the content to a div, a JQuery event placed in a partial view's document.ready function executes bef ...

Adding images to chart labels in vue-chartjs explained

I'm facing a challenge in adding flag icons below the country code labels, and I could really use some guidance. Here is an image of the chart with my current code The flag images I need to use are named BR.svg, FR.svg, and MX.svg, located under @/a ...

Invoking PHP code from within Javascript will output the function as a direct string

I seem to be going in circles and missing something silly... My setup involves using CodeIgniter on the server-side and Bootstrap on the client, but that's not really the issue here... I am attempting to access a PHP value within a JavaScript functi ...

The custom error message E11000 is specific to MongoDB's unique index constraint

How can a custom error message be specified for the 'E11000 duplicate key error' in MongoDB? (If possible, using Mongoose): userSchema.index({ name: 1, email: 1 }, { unique: true }); ...

Extract form input to utilize in nodemailer

Currently I am working on a form implementation where I intend to utilize nodemailer for sending test emails to myself. My goal is to have the email inputted into the form dynamically appear in both the "to" and "subject" fields when the user submits the f ...

What is the method for accessing a map that has been set in a separate component?

My current setup involves utilizing a Leaflet map with vue2leaflet. The process I follow is quite standard: I import a list of places from a REST Api in the app store (vuex) Following that, during map initialization, the markers are created using the in ...

Searching through an array based on a specific string

Could someone lend a hand in getting this to function... The code snippet below is functioning const acco = [{FullyQualifiedName=(-) Imposto Unico, Id=109, sparse=true, AcctNum=3.1.2.01.03027}, {FullyQualifiedName=13º Salário, Id=114, sparse=true, AcctN ...

"Encountered a 'NextAuth expression cannot be called' error

Recently, I delved into learning about authentication in Next.js using next-auth. Following the documentation diligently, I ended up with my app/api/auth/[...nextauth]/route.ts code snippet below: import NextAuth, { type NextAuthOptions } from "next-a ...

In JavaScript, when a div element contains an iframe, the click event will not trigger on the div

Check out this sample HTML markup: <div> <iframe></iframe> </div> The iframe is set to fill the outer div. I've added an event handler to the div click event: div.addEventListener("click", function () { console.log( ...

Include @url.action with a parameter on a cell within a table using AJAX

I am new to this and I want to add an action method on a table cell. The challenge is that the table is generated using JavaScript (AJAX). Here's the code: $.ajax({ url: "GetData", contentType: "application/json; charset=utf-8 ...

I'm attempting to retrieve external XML data and showcase it on my HTML page

My goal is to extract any xml tag from the provided sample. However, I am facing an issue with Internet Explorer where I can only retrieve data from the first record. What kind of javascript code should I implement to resolve this problem? <html> ...

The three.js raycaster is able to detect objects both in front of and behind the specific object I am trying to select

I am currently working on rendering a 3D model of a house using Three.js and encountering an issue with the raycaster in my code. The problem I'm facing is that it's selecting every object both behind and in front of the specific object I want to ...

Unable to locate 'this' in the callback function triggered by tinymce

I am experiencing an issue with my Angular2 app that uses tinyMce as an HTML editor. The problem arises when I need to convert the URLs in the HTML content using my REST API. I tried using the "urlconverter_callback" function from tinyMce, but I encountere ...

I am selecting specific items from a list to display only 4 on my webpage

I am trying to display items from a list but I only want to show 4 out of the 5 available items. Additionally, whenever a new item is added, I want it to appear first on the list, with the older items following behind while excluding the fifth item. Despi ...

Even when only a handful of modules are utilized, Webpack still imports a significantly large existing chunk

Currently, I am working on enhancing the splitChunks configuration in a multi-page application that utilizes Webpack 5, with a PHP backend and a Vue frontend. To optimize the vendor file size, I have started customizing the test function of the vendor cac ...

Picking out specific SharePoint components using jQuery

I encountered an issue with making my two radio boxes readonly. To resolve this, I attempted to disable the unchecked option. Despite trying to access the elements using jQuery and reviewing the data from the console, the solution did not work as expected. ...

Ways to verify if a variable holds a JSON object or a string

Is it possible to determine whether the data in a variable is a string or a JSON object? var json_string = '{ "key": 1, "key2": "2" }'; var json_string = { "key": 1, "key2": "2" }; var json_string = "{ 'key': 1, 'key2', 2 } ...

One way to concatenate texts in an array of dictionaries while keeping the order intact in JS/React is to compare and match specific key-value pairs

My dataset looks something like this: Record= [ { "tid":1, token_text": "Canis", "spanid": 1, "label": "Name" }, { "tid":2, "token_text": "Familiaris", "spanid": ...

Is it possible to pass a useState function to a component during its initialization?

Currently, I am utilizing the useState hook to effectively handle the rendering of components on the screen. My goal is to initialize it with a component while passing in the useState function to set the screen within the component. Below is my App.js fil ...

Is it possible for links to remain clickable even if they pass underneath a div

I have implemented a shadow using a div element to cover some content beneath it. However, I am facing an issue where the div that is under the shadow cannot be interacted with. Is there any solution to this problem? Thank you ...