0

I have a simple express route that GETs a user from mongo

routes.js

router.get('/users/:username', userController.read);

user.controller.js

function read(req, res) {
    User.findOne({
        username: req.params.username
    }, function(err, user) {
        if (err) res.status(404).json(err);
        res.status(200).json(user);
    });
}

I ran some tests through Postman and supertest

here's the supertest call

it('should retrieve user data', function(done) {
    request(BASE_URL)
        .get('/users/' + testUserData.username)
        .expect(200)
        .expect({
            username: testUserData.username,
            email: testUserData.email
        }, done)
})

The user's data comes back fine but the status code is 201 instead of 200.

Yoni Levy
  • 1,562
  • 2
  • 16
  • 35
  • Where's the rest of your code, and how are you invoking it (what's the cURL call, for instance)? In the interest of making sure you know what your own code is doing, take out the Mongo call. If you simply use `res.status(200).json({result: "sure, checks out"})`, what happens? These are debugging steps you should be running through to figure out *where* the problem is, so you can do targeted research, and writing a detailed question. – Mike 'Pomax' Kamermans May 07 '16 at 21:29
  • 201 my be returned by "post" routes so also make sure you are actually getting to the "get"... – It-Z May 07 '16 at 21:32
  • @Mike'Pomax'Kamermans I added more data to the question – Yoni Levy May 07 '16 at 21:43
  • @It-Z isn't getting the users data showing me I'm getting to the "get"? that's the only place I'm returning the user's data – Yoni Levy May 07 '16 at 21:45
  • @YoniLevy can you reproduce this *without* the mongodb call in there? – Mike 'Pomax' Kamermans May 07 '16 at 21:51
  • Yes. I did as you suggested (forgot to mention it, sorry) and the result is the same – Yoni Levy May 07 '16 at 21:53

0 Answers0