Troubleshooting Error: "Object.values method not recognized in MongoDB mapReduce"

Here is an example document from my collection:

{
  "_id": "5a68a9308117670afc3522cd",
  "username": "user1",
  "favoriteItems": {
    "5a0c6711fb3aac66aafe26c6": {
      "_id": "5a0c6711fb3aac66aafe26c6",
      "name": "item1",
    },
    "5a0c6b83fd3eb67969316dd7": {
      "_id": "5a0c6b83fd3eb67969316dd7",
      "name": "item2",
    },
    "5a0c6b83fd3eb67969316de4": {
      "_id": "5a0c6b83fd3eb67969316de4",
      "name": "item3"
    }
  }
}

Currently, I am utilizing the mapReduce feature in MongoDB to calculate how many times each item is liked by users.

Below are the map and reduce functions I am using:

var mapFun = function () {
    Object.values(this.favoriteItems).forEach(item => {
        emit(item, 1)
    })
}

var redFun = function (item, values) {
    let count = 0
    for(let i = 0; i<values.length; i++) {
        count += values[i]
    }
    return count
}

However, I encountered an error stating that Object.values is not a function:

2018-01-27T13:58:08.494+0000 E QUERY    [thread1] Error: map reduce failed:{
        "ok" : 0,
        "errmsg" : "TypeError: Object.values is not a function :\n@:2:5\n",
        "code" : 139,
        "codeName" : "JSInterpreterFailure"
}

Answer №1

NodeJs may not support the ES2017 feature Object.values if you are using an older version.

To resolve this issue, you have a few options:

  1. Update your NodeJs version
  2. Transpile the code using babel
  3. Use Object.keys to get the keys and then utilize them to access the values,

For example, you can do it like this:

Object.keys(this.favoriteItems).forEach((key)=>{
   let value = this.favoriteItems[key]
})

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

"Why is the success handler in the AngularJS controller not being triggered by the $http service

Currently, I am in the process of working on an AngularJS application. After creating the basic structure of the application, I moved on to implementing routing in Angular. This involved successfully setting up routing so that data from previous pages coul ...

Error message encountered: "mongodb $ generating issue due to the positional operator's failure to locate the required match in the

I have been attempting to modify the data stored in my mongoDB database. I am looking to update all products by adding a new field called productName. The structure of my data is as follows: { "id": "12345", "products" ...

In what way can you establish a boundary for a border?

My goal is to create a 10x10 grid with randomly placed black boxes, but I am facing an issue in my game setup: https://i.sstatic.net/uKy06.png Currently, the 5 black boxes are generated randomly in a row, sometimes exceeding the border and breaking the c ...

The issue of Angular finalize not functioning when used within a pipe alongside switchMap

Once the user confirms, I want the process to begin and keep the modal busy until it's complete. However, the current code does not function in this manner. The isModalBusy condition only turns false when an HTTP error is returned from the service. In ...

React Native ScrollView ref issue resolved successfully

I'm trying to automatically scroll to the bottom of a flatlist, so here's what I have: const scrollViewRef = useRef(); //my scroll view <ScrollView ref={scrollViewRef} onContentSizeChange={() => { scrollViewRef.current.scr ...

Unlock the powers of Express, Passport, and Redis sessions!

Lately, I have been utilizing the default MemoryStore for my Express sessions and everything has been running smoothly. However, I encountered a setback where all session data was lost between restarts. To address this issue, I am now attempting to configu ...

My JavaScript code seems to be malfunctioning as the content from my two-question form is not appearing within the central div. Is there anyone available who can help troubleshoot and correct my JavaScript

Can someone help correct my JavaScript code and explain the necessary steps taken to do so? I'm trying to figure out what mistake caused my quiz questions and options to not display correctly within the central div. Giving some background on my code: ...

Combining the functions .save and .create within a single asynchronous function

Here is the code snippet I am using to save data in a MongoDB database: const mongoose = require('mongoose') const sampleschema = new mongoose.Schema( { name:{ type:String }, age:{ type:Number ...

Attempting to retrieve data from local server 3000 to local server 3001 resulted in an unexpected termination of the input stream

After attempting to make a GET request to the Serp API directly from a React application, I discovered that it was not possible. To work around this issue, I created a local server and implemented the necessary logic. After testing the server using Postman ...

Using AJAX to search through paginated pages is quite simple and effective

Currently, I have developed a JavaScript script that filters names within a list. However, a problem has arisen with pagination - as the list paginates, I am unable to retrieve names without refreshing the page. Is there a way to use AJAX to access and dis ...

Combining React with Typescript allows for deep merging of nested defaultProps

As I work on a React and Typescript component, I find myself needing to set default props that include nested data objects. Below is a simplified version of the component in question: type Props = { someProp: string, user: { blocked: boole ...

Error on Android WebView: 'console' is undefined

While attempting to open an HTML file with JavaScript in an Android Webview, I encountered an error after implementing the onConsoleMessage of my WebChromeClient: Uncaught ReferenceError: console is not defined -- From line 10 of file:///storage/sdcard0/T ...

jQuery Dialog interface endlessly scrolls to the top

While debugging code for a project, I came across the use of jquery UI Dialog for popups. However, there seems to be an issue where the page keeps scrolling to the top while the dialog remains stationary wherever it was opened. Below is the code snippet in ...

Having trouble with a React child component not reacting to a click event?

The current issue I am experiencing is that the component is not triggering a click event when the button is clicked. TickerPage render() { return ( <div className="TickerPage"> <div className="TickerPage-container"> <b ...

Develop a plugin architecture using JavaScript and Node.js

Here is a basic implementation using node.js with express: var express = require('express'); var app = express(); app.get('/', function(req, res){ res.send('Hello World'); }); app.listen(3000); I am interested in creatin ...

Link the source data within a v-for loop to the props value

I have brought in several JSON files containing different sets of data. For my parent.vue input, I aim to iterate through these JSON files dynamically. <div v-for="(item, index) in <!-- JSONFile + Rank -->" :key="index"> T ...

jQuery character counting malfunctioning

Hey there I'm currently working on creating a jQuery word counter, but unfortunately most of them do not recognize special characters like æ, ø, å, ö, which are actually considered as LETTERS in my country rather than special characters. The fir ...

Using Redux to add an object to an array nested within another array

Hey there! I'm facing an issue with creating, updating, or deleting {note} in the [notes] array based on the operationId in the parent array id. I've tried using ADDNOTE but it's not working as expected. The problem is that without these fun ...

Ways to mix up a term while maintaining the original first and final characters intact (Javascript)

I've been given a task to shuffle a word that has more than 3 letters while keeping the first and last letters unchanged. The revised word should not be identical to the original, ensuring some sort of rearrangement is apparent. For example, when sh ...

Alternate routing based on conditions in Angular

I've used the "$urlRouterProvider.otherwise('{route here}')" syntax in angular to create a catch-all route in Angular UI-Router. One thing I'm curious about is whether it's possible to have conditional "otherwise" routing based o ...