1

Need some help here as I am very lost in something that should be very simple. Either that or I am losing my mind.

Ok so I am routing my routes using the express.Router()

I will jump right to the problem:

This works:

apiRouter.get('/api/user/:id', secureAPIPages, function(req, res, next){
    userModel.findOne({'profileID':req.params.id}, function(err, user) {
        if(user){
            res.json({
                fullname:   user.fullname,
                about:      user.about,
                birthday:   user.birthday,
                email:      user.email,
                location:   user.location
            });
        } else {
            console.log('Result does not exist');
       }
    });
})

So when I make a call on a browser like: http://localhost:3000/api/user/123456

It picks up the "id" variable and puts it in the req.params so all good there.

Now, the problem (this is what I want to make work, the above one is just to test out if my API routing worked):

apiRouter.get('/api/user', secureAPIPages, function(req, res, next){
    userModel.findOne({'profileID':req.params.id}, function(err, user) {
        if(user){
            res.json({
                fullname:   user.fullname,
                about:      user.about,
                birthday:   user.birthday,
                email:      user.email,
                location:   user.location
            });
        } else {
            console.log('Result does not exist');
       }
    });
})

Why is it not picking up my "id" and "name" variables when I run this URL on a browser: http://localhost:3000/api/user?id=123456789&name=Shayan

As always, thanks for the help in advance.

Shayan

1 Answer 1

2

You should use query object as follows:

If this is the URL:

http://localhost:3000/api/user?id=123456789&name=Shayan

use

req.query.id, req.query.name

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.