From 4bf1607f1da14953a0e0ee2f1c3ad2125a238a0e Mon Sep 17 00:00:00 2001 From: nathanallen Date: Thu, 17 Dec 2015 13:39:15 -0800 Subject: [PATCH 1/7] use findOneAndUpdate convenience method --- server.js | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/server.js b/server.js index ee62ca2..67958d0 100644 --- a/server.js +++ b/server.js @@ -103,14 +103,9 @@ app.get('/api/list/:_id', function getListData (req, res) { }); app.put('/api/list/:_id', function updateListData (req, res) { - db.List.findById(req.params._id, function (err, foundList) { - var updatedList = foundList; - updatedList.name = req.body.name; - updatedList.description = req.body.description; - updatedList.verbs = req.body.verbs; - updatedList.save(function (err, savedList) { - res.send(updatedList); - }); + // http://mongoosejs.com/docs/api.html#query_Query-findOneAndUpdate + db.List.findOneAndUpdate({_id: req.params._id}, req.body, function (err, updatedList) { + res.send(updatedList); }); }); From eaedf5a49bcf1705b056b8fc6437b34dd479b6a1 Mon Sep 17 00:00:00 2001 From: nathanallen Date: Thu, 17 Dec 2015 13:45:00 -0800 Subject: [PATCH 2/7] handle db errors in route gracefully: 404 + error message --- server.js | 51 +++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 41 insertions(+), 10 deletions(-) diff --git a/server.js b/server.js index 67958d0..3a23ddb 100644 --- a/server.js +++ b/server.js @@ -37,14 +37,20 @@ app.get('/lists', function homepage (req, res) { app.get('/api/verbs', function verbList (req, res) { db.Verb.find({}, function (err, success) { - if (err) { return console.log(err); } + if (err) { + console.log(err) + return res.status(404).json({errors: ["Bad Thing", err.message]}) + } res.send(success); }); }); app.post('/api/verbs', function newVerb (req, res) { db.Verb.create(req.body, function (err, success) { - if (err) { return console.log(err); } + if (err) { + console.log(err) + return res.status(404).json({errors: ["Bad Thing", err.message]}) + } res.send(success); }); }); @@ -53,7 +59,10 @@ app.post('/api/verbs', function newVerb (req, res) { // searching by infinitive app.get('/api/verbname/:infinitive', function verbList (req, res) { db.Verb.find(req.params, function (err, success) { - if (err) { return console.log(err); } + if (err) { + console.log(err) + return res.status(404).json({errors: ["Bad Thing", err.message]}) + } res.send(success); }); }); @@ -61,14 +70,20 @@ app.get('/api/verbname/:infinitive', function verbList (req, res) { //searching by infinitive ID app.get('/api/verbid/:_id', function verbList (req, res) { db.Verb.findById(req.params, function (err, success) { - if (err) { return console.log(err); } + if (err) { + console.log(err) + return res.status(404).json({errors: ["Bad Thing", err.message]}) + } res.send(success); }); }); app.put('/api/verbid/:_id', function updateVerb (req, res) { db.Verb.findById(req.params, function (err, success) { - if (err) { return console.log(err); } + if (err) { + console.log(err) + return res.status(404).json({errors: ["Bad Thing", err.message]}) + } var selectedTense = req.body.tense; var updatedVerb = success; //Checks to see whether anything has actually been changed. If there is a difference, it will save it. @@ -90,14 +105,20 @@ app.put('/api/verbid/:_id', function updateVerb (req, res) { app.get('/api/list', function listsList (req, res) { db.List.find({}).populate('verbs').exec(function (err, success) { - if (err) { return console.log(err); } + if (err) { + console.log(err) + return res.status(404).json({errors: ["Bad Thing", err.message]}) + } res.send(success); }); }); app.get('/api/list/:_id', function getListData (req, res) { db.List.findById(req.params._id).populate('verbs').exec(function (err, success) { - if (err) { return console.log(err); } + if (err) { + console.log(err) + return res.status(404).json({errors: ["Bad Thing", err.message]}) + } res.send(success); }); }); @@ -105,23 +126,33 @@ app.get('/api/list/:_id', function getListData (req, res) { app.put('/api/list/:_id', function updateListData (req, res) { // http://mongoosejs.com/docs/api.html#query_Query-findOneAndUpdate db.List.findOneAndUpdate({_id: req.params._id}, req.body, function (err, updatedList) { + if (err) { + console.log(err) + return res.status(404).json({errors: ["Bad Thing", err.message]}) + } res.send(updatedList); }); }); app.post('/api/list', function newList (req, res) { db.List.create(req.body, function (err, success) { - if (err) { return console.log(err); } + if (err) { + console.log(err) + return res.status(404).json({errors: ["Bad Thing", err.message]}) + } res.send(success); }); }); app.delete('/api/list/:_id', function removeList (req, res) { db.List.remove(req.params, function (err) { - if (err) { return console.log(err); } + if (err) { + console.log(err) + return res.status(404).json({errors: ["Bad Thing", err.message]}) + } res.send("List deleted."); }); }); // listen on port 3000 -app.listen(process.env.PORT || 3000); \ No newline at end of file +app.listen(process.env.PORT || 3000); From 1f31844274aa4660dde879f1e1863ab8835b1d7e Mon Sep 17 00:00:00 2001 From: nathanallen Date: Thu, 17 Dec 2015 13:48:25 -0800 Subject: [PATCH 3/7] whitespace --- public/app.js | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/public/app.js b/public/app.js index e240649..66f3a06 100644 --- a/public/app.js +++ b/public/app.js @@ -1,7 +1,7 @@ //GLOBAL VARIABLES //Functions are at the bottom. -var editListModalButtonHtml = +var editListModalButtonHtml = '
' + ' ' + '
'; @@ -265,7 +265,7 @@ $(document).ready(function() { $("span.new-verb.vous").html(vous); $("span.new-verb.ils").html(ils); }); - + $(".next-tense").click(function (event) { $(".save-tense").show(); $(".next-tense").hide(); @@ -347,7 +347,7 @@ $(document).ready(function() { //Gets all of the list information on page load getLists(); - + $("#newList").submit(function (event) { event.preventDefault(); @@ -407,7 +407,7 @@ $(document).ready(function() { }); $("#editListModal").on("click", "#addVerb", function () { - //Removes the #addVerb button. It will be re-added to + //Removes the #addVerb button. It will be re-added to //the bottom of the modal once a new verb has been added. $(".addVerb").remove(); //Calls a function that adds a dropdown that is populated with the verbs @@ -486,7 +486,7 @@ function renderConjugation(verb) { if ($(".verb-conjugation")) { $(".verb-conjugation").empty(); } - var conjugationGridHtml = + var conjugationGridHtml = '
' + '
' + '
' + @@ -498,7 +498,7 @@ function renderConjugation(verb) { ' ' + verb.tense.present.nous + '' + '
' + '
' + - '

Tu

' + + '

Tu

' + ' ' + verb.tense.present.tu + '' + '
' + '
' + @@ -560,7 +560,7 @@ function conjugateVerb(infinitive, tense) { $("input.new-verb.nous").val(stem + "ons"); $("input.new-verb.vous").val(stem + "ez"); $("input.new-verb.ils").val(stem + "ent"); - + } else if (family === "re") { $("input.new-verb.je").val(stem + "s"); $("input.new-verb.tu").val(stem + "s"); @@ -568,7 +568,7 @@ function conjugateVerb(infinitive, tense) { $("input.new-verb.nous").val(stem + "ons"); $("input.new-verb.vous").val(stem + "ez"); $("input.new-verb.ils").val(stem + "ent"); - + } else if (family === "ir") { $("input.new-verb.je").val(stem + "is"); $("input.new-verb.tu").val(stem + "is"); @@ -594,7 +594,7 @@ function conjugateVerb(infinitive, tense) { $("input.new-verb.nous").val(stem + "ions"); $("input.new-verb.vous").val(stem + "iez"); $("input.new-verb.ils").val(stem + "aient"); - + } else if (family === "re") { $("input.new-verb.je").val(stem + "ais"); $("input.new-verb.tu").val(stem + "ais"); @@ -602,7 +602,7 @@ function conjugateVerb(infinitive, tense) { $("input.new-verb.nous").val(stem + "ions"); $("input.new-verb.vous").val(stem + "iez"); $("input.new-verb.ils").val(stem + "aient"); - + } else if (family === "ir") { $("input.new-verb.je").val(stem + "issais"); $("input.new-verb.tu").val(stem + "issais"); @@ -626,7 +626,7 @@ function newVerbButtonCheck(tenseCount) { } function renderListData(list) { - var listHtml = + var listHtml = "
" + "
" + "
" + @@ -652,7 +652,7 @@ function renderListData(list) { " " + " " + " " + - "
" + + "
" + "
" + "
" + "
"; @@ -674,7 +674,7 @@ function listOfVerbsToEdit(verb) { } function renderVerbDropDownMenu() { - var dropDownMenuHtml = + var dropDownMenuHtml = '