0

I've just started learning javascript around a week ago with the help of Eloquent Javascript, and while it is a really good book, I could never learn and understand concepts just by reading, so I thought I'd start experimenting in nodejs, and following tutorials.I also intend to learn to use MySQL along the way, so I decided to combine them, and as most guides talk about mongoDB, I have to mostly figure MySQL out myself along with Js.

Anyway, here's a part of my main.js:

apiRoutes.get('/users', function(req, res){
    var userlist = Userdb.listusers();
    //res.json(userlist); ??
    //res.json(userlist.results); ??
});

And here's a part of my userdb.js which will interact with my mysql server.

var Userdb = {
    newuser: function(username,userpassword) {
        connection.query("INSERT INTO " + config.dbtable + " (name,passwd) VALUES (\'" + username + "\',\'" + userpassword + "\');");
    },
    listusers: function() {
        connection.query('SELECT * FROM testing;',function(err, results, fields){
            if (err) throw err;
            console.log(results);
            //return results? 
        });
    }
}

module.exports = Userdb;

And this is pretty much where I'm stuck, I need to access the results I get in my listusers method from my main.js, and I need an example, or something that looks like this, as I haven't found anything similar. Any help would be appreciated!

Also please note that, I intend to learn the languages and syntaxes my way, so I would like to use as few node apis as possible, even if other solutions would be easier/simpler. However suggestions are still welcome!

tamasfe
  • 157
  • 1
  • 8
  • 2
    If you just started learning Javascript, I don't think I'd recommend jumping right into Node.js and database management. Starting with simple DOM manipulation would probably be a more reasonable starting point. – Carcigenicate Feb 07 '17 at 18:54
  • And if the second argument to `query` is a callback, you'll need to learn to return values from an async function. Long story short: use a promise or some other construct made for this purpose. Query will return immediately; before the callback is executed, so you can't rely on typical sync code behavior. – Carcigenicate Feb 07 '17 at 18:55
  • Actually, yes, that's what I'm looking for, I just didn't know how to search for it yet, thanks a lot! – tamasfe Feb 07 '17 at 19:05

0 Answers0