458806 (1) [Avatar] Offline
#1
To anybody having trouble with Express in Action: Chapter 8,

The example program in Express in Action: Chapter 8 was not working for me, initially. However, I managed to get it working with a bit of tinkering! I thought I would share what I did, in case others are having trouble.

For starters, I added C:\Program Files\MongoDB\Server\3.4\bin to my environmental path variable in Windows, so I could just enter mongod in a command prompt window and then mongo in a second command prompt window to get my MongoDB working on local port 27017. Explaining how to do this in detail might be a good thing to go over in the book!

For Express in Action: Chapter 8, your Express app won't work until you finish at least all of the code up until Listing 8.28, on page 142. This confused the heck out of me when I first read this chapter! The author really needs to make this crystal-clear to first-time readers at the beginning of Express in Action: Chapter 8!

Once you have your Express app coded up to this point, you need to open a third command prompt window and enter npm start to get your Express app working and running on local port 3000. Then, you just need to visit local port 3000 in a browser of your choice to use your Express app. Explaining how to do this in detail might also be a good thing to go over in the book!

Also, there is a problem at the end of Express in Action: Chapter 8, too. I fixed it though! Here is what I changed for Listing 8.32 POST /edit (in routes.js) in Express in Action: Chapter 8:

Note: Code on top is new and good code from my brain and code on bottom is old / bad code from the textbook.

// now working! :D
// new code from my brain: 
router.post("/edit", ensureAuthenticated, function(req, res) {
	var userID = req.user._id;
	var newDisplayName = req.body.displayname;
	var newBio = req.body.bio;
	User.update({ _id: userID }, { $set: { displayName: newDisplayName, bio: newBio }}).exec();
	req.flash("info", "Profile updated!");
	res.redirect("/edit");
});


// old code from textbook (not working):
router.post("/edit", ensureAuthenticated, function(req, res, next) {
  req.user.displayName = req.body.displayname;
  req.user.bio = req.body.bio;
  req.user.save(function(err) {
    if (err) {
      next(err);
      return;
    }
    req.flash("info", "Profile updated!");
    res.redirect("/edit");
  });
});


PS: I hope all of this helps somebody!

PPS: I wish the book Express in Action covered more of MySQL and how it works with Sequelize (see page 121, just above Section 8.1.1, of Express in Action: Chapter 8).

PPPS: Other than these points, I am loving this book! smilie

Sincerely,
Andrew Nash
University Student