Approach for retrieving users by utilizing forEach loop in Mongoose

I have a list of profile IDs that looks like

get profiles [ '62d80ece61a85d738fa0c297', '62d80fb061a85d738fa0c29c' ]

I am trying to iterate through this list and use each ID in the find() method Here is my code snippet:

let total = [];

profile.forEach(async (index) => {
      console.log('index', index);

//Output from the console for index 
//index 62d80ece61a85d738fa0c297
//index 62d80fb061a85d738fa0c29c

      let user = await this.userModel.findOne({ _id: index });
      total.push(user);
 console.log('Service', total);
//The output from Service is []

Thank you in advance

Answer №1

In the absence of schema details, one potential workaround could be to directly convert the string into an ObjectId.

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

In Doctrine Mongo, when a child entity is the owning side, cascading removal can be

My current setup involves a parent/child relationship where one parent can have many children: /** * @ODM\Document */ class Parent { // ... /** * @var \Doctrine\Common\Collections\ArrayCollection * @ODM\R ...

Animating colors with jQuery and shifting SVG shapes to create dynamic and

I am currently working on an svg animation that involves changing the color of the svg to a different color, creating a running light effect. Rather than fading the fill color of the entire svg as commonly seen in examples, I aim to achieve a dynamic trans ...

The dropdown menu vanishes from sight as soon as the cursor moves away from a link

Recently, I encountered an issue while trying to create a dropdown menu using Jquery. The problem arose when attempting to select the second link, as the entire menu would disappear. Additionally, is there a way to ensure that only one dropdown menu is vis ...

What is the best way to showcase hexadecimal values of a byte array?

Hello, I am currently developing an application in C#. I have a byte array that contains hex values. My goal is to write these values directly to a file without converting them into strings or any other data type. Any assistance on accomplishing this tas ...

Enabling access to process and process.env in the latest version of Webpack, version 5

I'm currently facing an issue with setting the basename of a react-router-dom BrowserRouter using environment variables. Unfortunately, Webpack 5 does not support accessing environment variables directly. After researching on Stack Overflow, I came a ...

The function within the loop will only execute one time

I am facing an issue with a JavaScript function that is looping through my data. I have another function called inside the loop, but it only executes once on the last index. The code looks like this and I want this function to execute every time. Insid ...

Develop a scrambled PHP webpage for a CAPTCHA system

I've implemented the cool-captcha feature in my registration form. Here's the code snippet that generates the Captcha image: <img src="captcha.php" id="captcha" /> However, there is an issue where anyone can easily access the "captcha.ph ...

Is it possible to selectively download specific collections from a MongoDB remote server to a local machine?

Is there a way to only download certain collections from MongoDB dump to my local machine instead of all? I want to download only specific collections. Below is the command I use for downloading a collection. mongodump -h $MONGODB_SERVICE_HOST -d countly ...

Unable to load circles on page refresh with amCharts Maps

I am experiencing an issue with the functionality of my amCharts maps. Whenever I refresh the page, the circles that are supposed to be displayed disappear. However, when I zoom in, the circles reappear. This problem has me puzzled, and I'm not sure ...

Display alert only when focus is lost (on blur) and a dropdown selection was not made

Utilizing Google Maps Places for autocompletion of my input, I am aiming to nudge the user towards selecting an address from the provided dropdowns in order to work with the chosen place. A challenge arises when considering enabling users to input address ...

Retrieve the initial span within a <div> by using the data-id

Looking to access the initial span element inside a div? To retrieve the DOM element of the div, use the following code snippet: helper: function(event,ui){ var dataId = $(this).data('id'); console.log($('[data-id=" ...

Unable to access functions from an external JavaScript file that is being utilized by the HTML file

Having an index.html file: <head> <title>JavaScript Fun</title> <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script> </head> <body> <p> ...

Sending an array of objects as a parameter to a stored procedure in SQL

I am working with 2 tables: payment (payment_id, otherCosts, GarageCosts) spareparts (payment_id, sparepartId, sparePartQty) In the payment table, the payment_id is autogenerated. In my C# asp.net application, I have an array of objects like this: { sp ...

A guide on adding two fields together in AngularJS and displaying the output in a label

I am facing a unique issue on my webpage. Including two inputs and a label in the page, I want the label to display the sum of the values entered into these two inputs. My initial attempt was as follows: Sub-Total <input type="text" ng-model="Propert ...

Issues with modifying values within a nested array/object structure

I have been trying different approaches to achieve my goal, including using functions like array_map, array_walk, nested foreach loops with get_object_vars, and working with json_decode/encode. Despite making some progress, I have not been able to reach th ...

Issue with Modal functionality in Internet Explorer 9

Check out the URL of a page that functions properly in Firefox, Chrome, and Safari, but is experiencing issues in IE 9. Upon page load, a modal displays a banner. The modal I am using can be found at Although the script appears to be correct, I have not ...

Unable to transmit information to the Delegate

Attempting to retrieve data from a Pokemon API using MVVM, the MainViewModel is used to make the request. When printing the pokemon array in the MainViewModel, the array is displayed. However, when passing the array to the delegate, it fails to show in the ...

manipulating data using javascript

I have an array of objects like this: var arr = [{'name':'one','age':24},{'name':'two','age':21}] Then I assigned this array to another variable var newVar = arr. Next, I modified the value of a ...

Formulate an array using a sequence of values within a specified range

Is it possible to load an Excel range object into an array? Let's say we have values 1, 2, and 3 in cells A5 to A7 of an Excel sheet. The code below attempts to do this: Public Sub Test() Dim Test As Range Set Test = Worksheets("Sheet1").Rang ...

Combining arrays based on the parent_id of each element in PHP

Has this question about PHP already been asked before? I couldn't find the answer. I have an array that contains id and parent id. $cats = [▼ 0 => array:4 [▼ "id" => 5 "parent_id" => 4 "category&quo ...