From e78238959da75e4574bb4bbd4dc1c4433d02785b Mon Sep 17 00:00:00 2001 From: kdam0 Date: Wed, 1 Feb 2017 20:55:12 -0500 Subject: better error handling and clean up --- assets/js/home_page.js | 32 +++++++++++++++++++------------- node_simple.js | 6 ++---- routes/index.js | 7 +++++-- 3 files changed, 26 insertions(+), 19 deletions(-) diff --git a/assets/js/home_page.js b/assets/js/home_page.js index b04832c..059444c 100644 --- a/assets/js/home_page.js +++ b/assets/js/home_page.js @@ -23,7 +23,7 @@ function main(){ } }); - + // TODO: move this function to a general helper class?? // for auto-complete var substringMatcher = function(strs) { return function findMatches(q, cb) { @@ -45,23 +45,29 @@ function main(){ cb(matches); }; }; - // get array of all courses from controllers + + + // get array of all courses from controller $.ajax({ url: '/auto-complete-courses', type: 'GET', success: function(data){ - // console.log(JSON.parse(data)); - var states = JSON.parse(data); + var jsonData = JSON.parse(data); + if (jsonData.success) { + var states = jsonData.data; - $('.typeahead').typeahead({ - hint: true, - highlight: true, - minLength: 1 - }, - { - name: 'courseNames', - source: substringMatcher(states) - }); + $('.typeahead').typeahead({ + hint: true, + highlight: true, + minLength: 1 + }, + { + name: 'courseNames', + source: substringMatcher(states) + }); + } else { // console log the error mssg + console.log(jsonData.data); + } } }); } diff --git a/node_simple.js b/node_simple.js index e991515..1061464 100644 --- a/node_simple.js +++ b/node_simple.js @@ -117,7 +117,6 @@ var uri = exports.uri = 'mongodb://general:assignment4@ds057862.mlab.com:57862/ exports.setupDB = function (callback) { mongoFactory.getConnection(uri).then(function (database) { db = database; - // var corses = exports.find_all_course_codes_from_exams(); callback(true, "Database connected"); // signal start of app }).catch(function (err) { @@ -659,10 +658,9 @@ exports.updatePassword = function (email, password, callback) { exports.find_all_course_codes_from_exams = function (callback) { var exams = db.collection('exams'); exams.distinct("course_code", function (err, result) { - if (err) throw console.log(err); + if (err) callback(false, "Failed to get unique courses"); else { - // console.log(result); - callback(result); + callback(true, result); } }); diff --git a/routes/index.js b/routes/index.js index 31f5442..04ee1ff 100644 --- a/routes/index.js +++ b/routes/index.js @@ -21,8 +21,11 @@ router.get('/', function(req, res, next) { router.get('/auto-complete-courses', function(req, res, next) { // console.log('in controller'); - dbFile.find_all_course_codes_from_exams( function (data) { - res.send(JSON.stringify(data)); + dbFile.find_all_course_codes_from_exams( function (success, data) { + var res_obj = {}; + res_obj.success = success; + res_obj.data = data; + res.send(JSON.stringify(res_obj)); }); }); -- cgit v1.2.3