diff options
| author | kdam0 <kumar.damani@mail.utoronto.ca> | 2017-02-02 01:55:12 +0000 |
|---|---|---|
| committer | kdam0 <kumar.damani@mail.utoronto.ca> | 2017-02-02 01:55:12 +0000 |
| commit | e78238959da75e4574bb4bbd4dc1c4433d02785b (patch) | |
| tree | b62a9302cf1e450953d1fddc64b368727b61086b | |
| parent | 98c9cbc6258f699fc944f025230c35faf8575bc2 (diff) | |
better error handling and clean up
| -rw-r--r-- | assets/js/home_page.js | 32 | ||||
| -rw-r--r-- | node_simple.js | 6 | ||||
| -rw-r--r-- | 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)); }); }); |
