Skip to content

Instantly share code, notes, and snippets.

@jaygraygray
Last active April 25, 2017 22:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jaygraygray/b1d916fdb94369fd2a9c3eea939ac1ac to your computer and use it in GitHub Desktop.
Save jaygraygray/b1d916fdb94369fd2a9c3eea939ac1ac to your computer and use it in GitHub Desktop.
Back end controller for handling all requests related to articles.
GetBookmarks : function(req, res) {
var results = []
//
// Get array of IDs for specific user
//
db.query("SELECT bookmarks_list FROM users WHERE id="+req.params.user_id, function(err, resp) {
if (err) {console.log(err)} else {
var bookmarks = resp[0].bookmarks_list.split(',')
//
// Select individual article
// based on each index of bookmarks array
//
for (var i = 0; i < bookmarks.length; i++) {
(function(i) {
db.query("select * from articles where id = " + bookmarks[i] + "ORDER BY id DESC",
function(err1, resp1) {
if (err) { console.log(err) } else {
if (i == bookmarks.length-1) {
results.push(resp1)
res.send(results)
} else {
results.push(resp1)
}
}
})
})(i)
}
}
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment