0

I am new to express and am trying to wrap my head around callbacks in RESTful actions. In my PUT request below, I'm confused about the following line that I have bolded below. Why is response.pageInfo.book being set to the second parameter in the anonymous function (result)? that seems kind of arbitrary.

Also, what is the best way to inspect some of these parameters (req, res, result, etc)? When I console.log it, doesn't show up in my terminal or in my browser console.

exports.BookEdit = function(request, response) {
    var id = request.params.id;

    Model.BookModel.findOne({
        _id: id
    }, function(error, result) {
        if (error) {
            console.log("error");
            response.redirect('/books?error=true&message=There was an error finding a book with this id');
        } else {
            response.pageInfo.title = "Edit Book";
            **response.pageInfo.book = result;**
            response.render('books/BookEdit', response.pageInfo)
        }
    })

}
1
  • I'm not sure why and what you think it is arbitrary. The book is set to the result of your findOne, and then passed with pageInfo to the rendering of the result, so why should it be arbitrary? console.log in your express application won't show up in the browser, because this code runs server side. If nothing shows up in the terminal where you run your express application then you most likely to the log at the wrong place. Commented Sep 20, 2015 at 17:08

2 Answers 2

1

The findOne function takes a query ({_id : id}) and a callback as arguments. The callback gets called after findOne has finished querying the database. This callback pattern is very common in nodejs. Typically the callback will have 2 arguments

  • the first one error is only set if there was an error.
  • the second one usually contains the value being returned. In this case you are finding one book in the database.

The line you have bolded is where the book object is assigned to a variable which will be sent back to be rendered in the browser. It is basically some javascript object.

Your second request, to debug this stuff, here is what you can do:

In you code type the word debugger;

e.g.

var id = request.params.id;
debugger;

Next, instead of running your program like this:

node myprogram.js

... run with debug flag, i.e.

node debug myprogram.js

It will pause at the beginning and you can continue by pressing c then Enter

Next it will stop at that debugger line above. Type repl and then Enter and you'll be able to inspect objects and variables by typing their names.

This works very well and requires no installation. However, you can also take a more visual approach and install a debugger such as node-inspector which does the same thing but in a web browser. If you use a good IDE (e.g. webstorm) you can also debug node.js pretty easily.

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

Comments

1

In the above, the document that is the result of the findOne() query is being added to the pageInfo key of the response and is then being rendered in a template. The first parameter is a potential error that must be checked and the remainder contain data. It's the standard node idiom that an asynchronous call returns to a callback where you do your work.

The writer of the code has also decided to decorate the response object with an extra attribute. This is often done when a request passes through a number of middleware functions and you might want to build up the response incrementally (for example having a middleware function that adds information about the current user to the pageInfo key).

Look and see what else is on response.pageInfo. Information was probably put there by previous middleware (especially since the function above expects the pageInfo key to exist). Just do a console.log(response.pageInfo) and look on your server log or standard out.

Comments

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.