0

I'm using the Express framework for my node application. I'm quite new to it so I thought I'd create a defacto "To-Do" application to learn about it. What I'm trying to do it log a request made for debugging purposes. So when I go to:

app.get('/todos/:id', function (req, res) {
    var result = db.load(req.params.id);
    result ? res.send(result) : res.send(404);
});

I want to a) see what result equals and b) log what happens in my db.load method:

exports.load = function (id) {
    todos.findOne({ id: id }, function (err, todo) {
        if (!err) {
            return todo;
        }
    });
}

I'm using the mongolian library to access my MongoDB data. I've followed an example by Steve Sanderson: https://github.com/SteveSanderson/nodejs-webmatrix-video-tutorials

2
  • What is your question? It is not clear why you can't just do what you described. Commented Sep 13, 2012 at 11:17
  • When I put a console.log where ever I put it, it doesnt appear, so I can put console.log("Hello") in my load method or get method and nothing appears Commented Sep 13, 2012 at 11:26

1 Answer 1

2
app.get('/todos/:id', function (req, res) {
    db.load(req.params.id, function(err, result) {
        // also handle err
        result ? res.send(result) : res.send(404);
    });
});


exports.load = function (id, callback) {
    todos.findOne({ id: id }, callback);
}
Sign up to request clarification or add additional context in comments.

3 Comments

Consider adding some explanation to your answer, since the asker is apparently confused by async control flow.
When I pass in req.params.id I get a 404, but if I hard-code a value (eg: 1) it'll return that object. If I put a console.log(req) to see that values, nothing appears?
Well.. I figured it out.. I had to do: db.load(parseInt(req.params.id)..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.