1

I am new to node js and Mongo Db. I am trying to edit the user details . So for that I displayed the user list with Edit link for each user. When I click on edit link I passed the name of the user to the router and I am successfully able to get it there. But failed to find the details of user based on that passed name. Below is my code

 router.get('/edit/:name', function(req, res){
  var username= req.params['name'];
  console.log(username);
  db.collection('users').find({ name:username }).toArray(function (err, result) {
        console.log(result);
        if (err)
            throw err;
            res.render("edit-form", {details: result})
    });
});

Here , when I do console.log(username); I got the username , but console.log(result); is empty. If I try to give the username directly within find method, just like below, I got the result in my page.

db.collection('users').find({ name:"test"}).toArray(function (err, result) {
        console.log(result);
        if (err)
            throw err;
            res.render("edit-form", {details: result})
    });

How can I dynamically get the username so that I can get the corresponding user result from database.

Oops
  • 1,373
  • 3
  • 16
  • 46

1 Answers1

0

It might be problem of deprecation of req.params['name']

You can directly get your param

var username= req.params.name ;

you need to also check you are not passing any extra space with your username.

 username.trim() 

then pass it to your find

Prakash Karena
  • 2,525
  • 1
  • 7
  • 16