Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8335243
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T03:36:59+00:00 2026-06-09T03:36:59+00:00

Im using node.js+express+mongodb+mongoose, trying to retrieve user from database by custom ids, page returns

  • 0

Im using node.js+express+mongodb+mongoose, trying to retrieve user from database by custom ids, page returns back completely empty due to incorrect datatype of id value, here is some part of my code:

var User = new Schema({
  name: String , 
  email: String ,
  id: Number,
  role: String
});
var userModel = mongoose.model('User', User);
var person = new userModel();

function loaduser(req, res, next) {
  userModel.find({}, req.params.id, function(err, user){
    if (user){
      req.user = user;
      console.log(user);
      next();
    }
  })
  console.log(typeof req.params.id);
};

//personal page route
app.get('/user/:id', loaduser, function(req, res){
  res.render('show', { users: req.user });
});

and then this returns in console:

string
{ _id: 5012aa222c2d4d5876c5acd0,
    email: 'alex@mail.com',
    id: '0',
    name: 'alex',
    role: 'admin' },

even though i declared type of id as Number it gets back as String.
Any suggestions are welcome 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T03:37:01+00:00Added an answer on June 9, 2026 at 3:37 am

    Your find call isn’t quite right. The first parameter should be your query document and since you’re looking for one document you should use findOne instead. You also need to convert req.params.id to a number:

    userModel.findOne({ id: Number(req.params.id) }, function(err, user){
    

    If you still see a problem with id coming back as a string, it must be a string in the collection.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using node.js with express. I read out data from MongoDB with Mongoose
i'm using node 0.4.11 express 2.4.6 mongodb 1.8.3 mongoose 2.1.2 connect-mongodb 1.0.0 and trying
I'm trying to displpay the data I retrieved from database(I'm using node.js express and
Using Jade + Express + Node.js + Mongoose + MongoDB for my app, but
I am using Node.js (with Express.js) to pass a JSON data object from the
I'm trying to set up a simple project using Node.js and Express with Expresso
I just installed mongodb from Synaptic (Using ubuntu 11.10). I'm following this tutorial: http://howtonode.org/express-mongodb
I have built my application using node.js, mongodb, redis, express, socket.io and planning to
Im having some issues with deleting an entry from mongo DB. Im using node-mongodb-native
I am using node and express. To register a controller I call: app.get('/user/:id', function

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.