Unable to locate item by its identification number

Search for results in the index and return them.

Method:

async fetchIndex(req,res){
        const userResults = await Usuario.find();
        res.json(userResults);
    },

Route:

routes.get('/api/usuarios', Usuario.fetchIndex);

Having trouble getting results with findOne({_id}). Any help would be appreciated (I have tried changing params to query, but it didn't work).

Method:

async fetchDetails(req,res){
        const { _id } = req.params;
        const userDetails = await Usuario.findOne({_id});
        res.json(userDetails);
},

Route:

routes.get('/api/usuarios.details/', Usuario.fetchDetails);

Answer №1

To start with, you must update your routing configuration

routes.get('/api/users/:_id', User.getDetails);

Next step is to make the API call as shown below

http://localhost:<your port number>/api/users/<user id>

Lastly, in your controller function

User.findOne(
   { _id: req.params._id }
)

Answer №2

async showDetails(req,res){
    const { id } = req.params;
    const member = await User.findOne({_id: id});
    res.json(member);
},

Give the code snippet a go!

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

Utilizing node.js, mongoDB, and express.js to upload and recover images

Is there a way to upload an image from my browser and save it as a local file while also adding the file name to mongodb? I want to be able to retrieve the stored file in the local folder and display it in the browser. Thank you, Babji ...

Redirecting after Form Submission

Is there a way to handle redirection after making a post request in Node.js and React? I've set up a route in my server.js file and I'm attempting to redirect using axios and rendering the view in the .then function. ...

Troubleshooting problems with sending data in Jquery Ajax POST Request

I've spent a considerable amount of time searching for a solution to why my post request isn't sending its data to the server. Oddly enough, I can successfully send the request without any data and receive results from the server, but when attemp ...

Unveiling the Truth about Svelte Transitions

Recently, I attempted to incorporate the guidance provided in this repl () for implementing flying and fading effects on divs. However, it seems that I may have a slight misunderstanding regarding the concept... To tackle this, I designed a component rese ...

The issue with Protractor's sendkeys function is that it requires an X display for converting keycodes

I've encountered an issue while attempting to execute Protractor e2e tests in a Vagrant VM using headless Chrome. Despite successfully configuring Xvfb, I face an error when trying to fill out a form: unknown error: an X display is required for keycod ...

React HTML ignore line break variable is a feature that allows developers to

Can you help me with adding a line break between two variables that will be displayed properly in my HTML output? I'm trying to create an object with a single description attribute using two text variables, and I need them to be separated by a line b ...

To show the image along with the .titleclass, .subtitleclass, and .description when clicking on it, what steps do I

<?php while($galleryrec = mysqli_fetch_array($getresultgallery)){ ?> <div class="col-lg-2 col-md-2 mb-4 mb-lg-0" id="imageidback"> <img data-id ="<?php echo $galleryrec['publishid& ...

How can you preselect an item in Vuetify's item group?

When attempting to preselect an item from a vuetify item-group, I discovered that it works with strings but not with objects. The vuetify documentation shows using a string array as the item list for the item-group, which functions correctly. However, whe ...

Using Ubuntu, AWS EC2 Instance unable to start Node.JS Express App

For the past 24 hours, I have been struggling to get my express app up and running. Despite following tutorials and reinstalling everything, I still can't seem to make it work. If you're curious about the M.E.A.N stack installation process that ...

Day Change Triggers React [Native] View Update

I've been working on a React Native component that needs to update itself when the day changes, regardless of user interaction. Is there another method besides using setInterval for this task? If I use setTimeout in my viewDidLoad function with the n ...

Waiting for Capybara to wait for the AJAX to finish loading

Hello, I am experiencing an issue with my capybara testing environment. Error: jQuery is not defined (Session info: chrome=43.0.2357.125) I suspect that this problem may be related to the ajax wait function. def wait_for_ajax Timeout.timeou ...

Creating a RESTful API

To begin with, I am a newcomer to web frameworks and we are currently using Meteor. In our database, we have a collection of Students: Students = new Mongo.Collection('students'); At the moment, we have defined a Rest API as follows: // Maps t ...

Utilizing Visual Studio: Implementing jsmin in post-build actions

After attempting to add jsmin.exe as a post-build event in my VS 2010 project, I encountered an "error code 9009" when building the project. I tested this in the command prompt and found that it works if I navigate to the folder and run: jsmin < debug ...

Ways to obtain parameter from URL in Express without diving into the request object

const express = require('express'); const app = express(); app.use('/', anyroute); // in anyroute file router.route('/:id').get(controlFunction) controlFunction(req, res, res)=> { // Here we can get the "id" fr ...

Revamping the hyperlinks in my collapsible menu extension

Is there a way to modify the links in this accordion drop menu so that they lead to external HTML pages instead of specific areas on the same page? I've tried various methods but can't seem to achieve it without affecting the styles. Currently, i ...

Unable to Access ReactJS Nested Property in JSON Data

While attempting to access a nested property in my JSON file loaded into the state, I encountered an issue. Despite confirming the existence of the property within the object through logging, when trying to navigate a level deeper using dot-notation, an er ...

I sought out a way to incorporate a hyperlink within the Material Data Grid

Take a look at this sample data grid for reference: here. I added an image like this: see here. However, I couldn't create a clickable link. ...

Unable to upgrade the Expo SDK version is not an option

I've been working on a project with React Native Expo, and I was testing it using Expo Go on my Android phone. However, after updating the mobile app, I encountered an issue when trying to scan the QR code to test the app. It turns out that the Expo S ...

Learn how to trigger a re-render in React to update a value stored in local storage

I need assistance in displaying a spinner with a percentage value during file uploads within my React application. To achieve this, I am utilizing Material UI's circular progress. My approach involves making a REST call using Axios to obtain the perce ...

I need assistance with extracting and passing all the row and column values from a Table in React js upon clicking, in order to transfer them to another Component. Any advice or guidance on how to achieve

After creating a custom table component to display data, I need to retrieve all the row values along with their headings upon clicking and then pass them to another component. Here is the code for my table component: import PlaceBett from "./PlaceBett ...