Using Mongoose to iterate through each item in a collection's array of values

I'm attempting to utilize the .find method in mongoose to retrieve all items in my collection. I then experimented with using a forEach loop to create a variable that stores this information and pushes it into an array.

However, I've encountered a problem where the code doesn't seem to work as expected. Despite checking for errors through console logging, nothing was being returned. Am I implementing this function correctly?

[ { _id: 5b7e933aa5b5e7165fd02e77,
id: 7151,
location: 'X: 151.1869; Y: -1006.755; Z: -98.99999',
price: 5000,
door: 'X: -2.0055665969848633; Y: 20.169893264770508; Z: 71.10984802246094',
__v: 0 },


{ _id: 5b7e96042ba33b18dd03740f,
    id: 6264,
    location: 'X: 151.1869; Y: -1006.755; Z: -98.99999',
    price: 5000,
    door: 'X: -405.9184265136719; Y: 1160.5306396484375; Z: 325.9139404296875',
    __v: 0 },
  { _id: 5b7e9664344f8c198f0fd266,
    id: 1694,
    location: 'X: 151.1869; Y: -1006.755; Z: -98.99999',
    price: 5000,
    door: 'X: -406.7716369628906; Y: 1162.4755859375; Z: 325.9158630371094',
    __v: 0 } ]

    House.find({}, function(err, house) {
        console.log(house);
        if(err) console.log(err);
        house.forEach(function(h) {
            let loadHouse = {
                id: h.id,
                location: JSON.parse(h.location),
                price: h.price,
                door: JSON.parse(h.door),
            }
    houses.push(loadHouse);
    console.log(houses);
});

Answer №1

It appears that the errors are located within the forEach loop, where you are attempting to utilize JSON.parse() with invalid JSON strings.

JSON.parse('X: -405.9184265136719; Y: 1160.5306396484375; Z: 325.9139404296875')

For now, it would be best to remove the JSON.parse().

(My apologies for posting this as a comment instead of an answer)

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

Angular2-starter configuration setup with environment-based variables (such as .env or .conf) for testing and production settings

Frameworks like PHP Laravel often include files for local configuration, separate from dev, test, and production environments. How can a configuration file be provided for an angular-starter project that contains all local environment variable values (su ...

Encountering a TypeError with DataTables and Tabledit

I've been attempting to integrate DataTables with Tabledit, but I keep encountering the error message "TypeError: Cannot set properties of undefined (setting 'nTf')". The number of tags also matches up. Interestingly, if I comment out the " ...

Is there a way to send data to a sibling component without the need to store it as a variable?

In my project, I am utilizing bootstrap vue cards within a v-for loop. Within this loop, one of the tags is supposed to return a value based on the current iteration of v-for. My challenge is that when I save this value as a variable and pass it to another ...

Is there a way to stop a modal from closing using setTimeout() in an Ajax Jquery function?

My goal is to have the index.php page automatically refresh and load data from a MySQL table every 2 seconds using setTimeout(). However, I am facing an issue where every time I click the View Details button on the second page (order.php), the modal loads ...

How can I prevent vuejs watch methods from being triggered when the value is modified by the same method?

monitorChanges: { observeValue() { setTimeout(() => { this.observeValue = ""; }, 4000); } } In this scenario, the observeValue function is initially invoked by the DOM, but it repeats when the value ...

What is the best way to add HTML content to a specific div element within an AJAX call's success function?

In the provided ajax script, my goal is to insert HTML content into a specific div element inside a table. The main div element in question is named "ajaxcom", but I need to insert the HTML content into a div element with the class = "divrep". How can I ac ...

Restore Bootstrap Dropdown values to their initial settings when clicked

I need a button that can reset all filter dropdown values to their default values. The current code I have only changes all values to "Filter" when reset, but I specifically need it to reset to "Car brand" and "Model". Here's my code: // set.... $(" ...

Problems with Web Audio API Implementation - struggling to make it functional

As a newcomer to the Web Audio API and not well-versed in JavaScript, I am trying to implement a specific function on a website that involves Google's TTS API. This function requires the returned Base64 audio to pass through a reverb filter and then a ...

Steps for organizing a particular key in a map

I need assistance sorting my map based on the "time" value, but so far, I haven't been successful in finding a solution after searching online extensively. If anyone has any insights or recommendations, please share them with me. This is how my Map ...

The combination of NodeJS Knox and Formidable led to a 400 error, causing the file to not be uploaded to the

I am encountering a 400 error while trying to utilize formidable and knox for uploading files to an AWS S3 bucket. Unfortunately, each attempt results in failure with the file not being uploaded. The code I am using is quite similar to this nodejs knox put ...

Incorporating React.js with a server API that doesn't use JavaScript

My upcoming project involves enhancing my current application frontend by implementing React.js. The existing frontend currently utilizes standard REST calls to communicate with a server built using Microsoft technologies, and there are no plans of changin ...

Tips on preventing users from navigating backwards in a React Redux application

Seeking guidance on how to restrict user access after successful login in react redux. Can anyone assist me? I want to prevent users from navigating back to certain routes once they are logged in I am currently working on securing the routes to block unau ...

Using JavaScript Functions to Resize Buttons and Change function on Click

I have a button in my code that triggers CSS changes by adding and removing classes. The JavaScript for this function works as intended - clicking once adds the class, clicking again removes it, and so on. However, I also implemented a feature to remove t ...

Error: Unable to deploy app on Heroku due to missing hostname, domain name, and top-level domain in URI

After successfully running my app on my local machine, I encountered a problem when deploying it on Heroku. I am unable to connect to my database and received the following error: MongoParseError: URI does not have hostname, domain name and tld. I have alr ...

Visual Studio 2013: Enhancing Your Javascript Development Experience

Recently, I purchased a Windows 8 PC and installed both Visual Studio 2013 Pro and Visual Studio 2013 Express. However, I am facing an issue with creating Javascript projects for developing apps specifically for tablets. Is there any additional software ...

Opting for a GET request over a POST request when interacting with a JSON RPC API

Can I send a GET request to a JSON RPC API? I'm attempting to do this with the random.org api (). It currently works with a POST request, but I need to use GET requests for all the APIs in my project. Below is the working POST request: function getNe ...

Having trouble retrieving prices using an npm package

There is a more effective way to retrieve prices using the npm package, node-binance-api, rather than relying on the "coin" variable that I am currently struggling with. If anyone could assist me in finding a better solution or the optimal method for fetch ...

Expanding the Element prototype with TypeScript

Is there a corresponding TypeScript code to achieve the same functionality as the provided JavaScript snippet below? I am looking to extend the prototype of the HTML DOM element, but I'm struggling to write TypeScript code that accomplishes this with ...

The functionality of JQuery TableSorter is not responsive when a user clicks on it

I seem to be running into an issue on my website where I can't get the JQuery TableSorter to work properly. When I attach it to a table for sorting, nothing happens when I click on the headers. The table remains static and unsortable. Here's a s ...

The TypeScript @types version's compatibility with the Library version is interconnected

Seeking clarity on the versioning system used for TypeScript @types. Visit https://github.com/DefinitelyTyped/DefinitelyTyped For instance: I presumed that if I was utilizing [email protected], then I would need to install @types/[email protecte ...