The code isn't able to make use of the MongoDB FIND method

When I attempt to search for items in a collection using the console with db.DB_NAME.find({ x: y }, it works perfectly. However, when trying to do the same in the code, it doesn't seem to work.

It's worth mentioning that the findOne method works flawlessly within the code.

Looking through the console

Code:

app.get("/api/customer/:cpf", async (req, res) => {
  const properties = await Customer.consult(req);
  console.log(properties);
  res.status(200);
  res.end();
});

static async consult(req) {
    const conn = await client.connect();
    const db = conn.db("website");
    const cpf = req.params.cpf.toString();
    console.log(cpf);
    const properties = await db.collection("Property").find({
      customerCpf: cpf,
    });
    return properties;
  }

Instead of receiving the expected three objects, what I'm seeing in my console from these code snippets is a large and perplexing object, which is confusing for me as a novice.

Answer №1

It seems like you are not utilizing mongoose.

When you use .find(), it returns a cursor. To work with it, you should convert it to an array:

const properties = await db.collection("Property").find({
  customerCpf: cpf,
}).toArray();

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

Node.js and the exchange of information between clients and servers

Recently, I've delved into the realm of node.js to create an educational online game. After perusing various tutorials, I managed to piece together the code showcased below. With this code, I've achieved client-server communication, which is esse ...

Tips for displaying a Rails action without a layout in html format using Ajax

Is it possible to render the new action without the application layout and without altering the current code structure? class FoobarController < ApplicationController def new @foobar = Foobar.new end # ... end When a user clicks on = link_ ...

Convert an array of objects into an object where the keys are determined by the value of a specific

I'm working with an array that looks like this: const inventory = [ { fruit: 'apple', quality: 'good', quantity: 10 }, { fruit: 'banana', quality: 'average', quantity: 5 }, { fruit: 'orange', qua ...

Response from Socket.io: What is the best way for the server to respond to all clients after receiving input from multiple clients?

Currently diving into the realm of node.js, express, and socket.io Thrilled to report that my server is up and running, successfully connecting to the browser via localhost:3000 Communication between client and server is seamless both ways. Now, onto th ...

Error Handling in ReactJS

Every time I try to execute my ReactJS code, an error pops up saying: Unhandled Rejection (Error): Material-UI: <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="90e2f5f1f3e4d0a1a6bea3bea0">[email protected]</a> ve ...

How do I ensure the CSS triangles maintain their correct boundaries when hovered over with the cursor?

Can the issue of incorrect triangle activation be fixed when hovering on http://jsfiddle.net/2AXhR/? Sometimes the wrong triangle is triggered due to the triangles' bounding areas being rectangles, causing overlap and z-index conflicts. <style ...

Is there a way to link to a specific section within a webpage using its ID, landing halfway down the page instead of at the top?

Can someone help me out with this issue? The header on my page is fixed at the top and has a height of 75px. When I click on a link (<a href="/company/#contact/">contact</a>), it takes me to the right section but the top part is hidden behind t ...

Refusing to handle HTTP requests in Node and Express API for MongoDB interactions

I am currently in the process of creating a basic API for performing CRUD operations on a local mongo database. Despite having what seems like correct code, the CRUD operations result in pending requests that never seem to complete. Below are snippets of ...

Tips for hiding a child component while navigating to a different React route

I've implemented a dropdown feature in my BasketContents component (as discussed on this thread). It fades in and out when a button is clicked. However, I also want it to automatically close when the route changes by clicking a Link tag. For example, ...

Cross-platform mobile browsers typically have scrollbars in their scrollable divs

I have successfully implemented scrollable div's on a page designed for tablets running Android 3.2+, BlackBerry Playbook, and iOS5+ (except iPad 1). However, I would like to add scrollbars to improve the user experience. For iOS5, I can use the suppo ...

Tips for adjusting the progress bar to 100% and back to 0%

My goal is to increase the progress bar percentage with each click. Currently, it only goes up to 75%. When saving, it should show 100%, but it's not displaying that properly. On clicking the back button, it should reset to 0% on the third click. HTM ...

Updating specific data in MongoDB arrays: A step-by-step guide

{ "_id":{"$oid":"5f5287db8c4dbe22383eca58"}, "__v":0, "createdAt":{"$date":"2020-09-12T11:35:45.965Z"}, "data":["Buy RAM","Money buys freedom"], & ...

The onClick function within the .map function is continuously triggered

I am encountering an issue with my code where a Dialog confirmation prompt from Material UI keeps getting called unexpectedly. The problem seems to arise when I add a value to the function that is triggered by a button click within a loop of an array usi ...

Dealing with alternating rows in a dynamic manner: tips and tricks

Exploring an example scenario: (view live demonstration) Sample HTML structure: <div class="wrapper"> <div city_id="1" class="odd">Street Name #1 in city 1</div> <div city_id="3" class="even">Street Name #2 in city 3</d ...

Dealing with event function inside object property: What's the best way?

Hello, I am currently using bootstrap-table for a web application where I need to redirect to another URL when a table row is clicked. The "onRowClick" events are managed through an object attribute known as onRowClick, where you assign it a function that ...

Hidden input for Vue form submission

I need to include two Id's with a user's input by adding them in a hidden input field. Since v-model doesn't work with hidden inputs, I have set up my hidden input like this: <input type="hidden" ref="property_id" :nam ...

Is it possible to create a single code that can be used for various buttons that perform the same function?

Whenever I click the right button, the left button appears and when I reach the last page, the right button disappears. However, this behavior is affecting both sections. Is there a way to write separate code for each section? I managed to make it work by ...

Transforming the output of a PHP script from an HTML table into an ion-list format tailored for the Ionic framework

Currently I am working on an application built with the Ionic framework. I have developed a PHP file to retrieve 'Users' data from my database and it is currently being displayed in an HTML table format. In the services section of the framework, ...

Troubleshooting issue with error handling in graphql mutation hook with react and apollo is not resolving

It seems like I might have overlooked a configuration in my code, but I can't seem to pinpoint where I went wrong. In our application, we have a basic login form. If the correct username and password are entered, everything works smoothly. However, ...

how to use jQuery to sort appended data

I have a loop that retrieves data from the server in descending order, but when I use "append()" to add the data, it doesn't maintain the correct sorting. For instance, the first row shows up in the second position. Is there a way to append the data ...