NW.js with MySQL - how to output?

887 views
Skip to first unread message

joegol...@gmail.com

unread,
Apr 14, 2015, 11:43:59 AM4/14/15
to nwjs-g...@googlegroups.com
Hi Guys,

I'm a total n00b with regards to Node.  I have some basic PHP skills with regards to connecting to MySQL databases, but I can't find any guides or tutorials which walk me through connecting to a MySQL database and retrieving data from it and listing it on the page.

I have a very simple task.  I have a database built using phpMyAdmin with a few tables within the database.  I just need to connect to the database and display the required rows/data on the screen.  Can anyone point me in the right direction?

I've seen some guides talking about node.js and node-mysql modules.  But these are totally throwing me off.  Are they the same thing?

I've been Googling for the last few days but can't find any guides which are easy to understand.  :(

Seth.

unread,
Apr 14, 2015, 1:01:44 PM4/14/15
to nwjs-g...@googlegroups.com
Use knexjs with nwjs 

-seth
--
You received this message because you are subscribed to the Google Groups "nw.js" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nwjs-general...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


--
Sent from Gmail iPhone App

Joe Goldenwell

unread,
Apr 15, 2015, 5:49:04 AM4/15/15
to nwjs-g...@googlegroups.com
Thanks seth,

But are there any n00b tutorials of how to get started with a full example?  I can't really seem to get the grasp of the syntax.

Thanks again!


On Tuesday, April 14, 2015 at 6:01:44 PM UTC+1, post2seth wrote:
Use knexjs with nwjs 

-seth

On Tuesday, April 14, 2015, <joegol...@gmail.com> wrote:
Hi Guys,

I'm a total n00b with regards to Node.  I have some basic PHP skills with regards to connecting to MySQL databases, but I can't find any guides or tutorials which walk me through connecting to a MySQL database and retrieving data from it and listing it on the page.

I have a very simple task.  I have a database built using phpMyAdmin with a few tables within the database.  I just need to connect to the database and display the required rows/data on the screen.  Can anyone point me in the right direction?

I've seen some guides talking about node.js and node-mysql modules.  But these are totally throwing me off.  Are they the same thing?

I've been Googling for the last few days but can't find any guides which are easy to understand.  :(

--
You received this message because you are subscribed to the Google Groups "nw.js" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nwjs-general+unsubscribe@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Seth.

unread,
Apr 15, 2015, 6:41:26 AM4/15/15
to nwjs-g...@googlegroups.com
Basically Knex is a query builder, you can write query in JS object way & got the callback, in case of run the sql query you can use knex.raw() function.

I will try to send you some code example with simple mysql connection & retrieving the data from db which you made using phpmyadmin.

here is the connection string.

var knex = require('knex')({
  client: 'mysql',
  connection: {
    host     : '127.0.0.1',
    user     : 'your_database_user',
    password : 'your_database_password',
    database : 'myapp_test'
  }
});
for the select statment

knex('users').where({
  first_name: 'Test',
  last_name:  'User'
}).select('id').then(function(res){// do your work here, //you can console res or parse it as json object})
here is the URL for more details



even you can email me directly at post2seth at gmail


P.S: I am not a committer or owner of knex, but i just love this framework.

-Seth



To unsubscribe from this group and stop receiving emails from it, send an email to nwjs-general...@googlegroups.com.

Joe Goldenwell

unread,
Apr 15, 2015, 7:12:28 AM4/15/15
to nwjs-g...@googlegroups.com
Thanks again Seth.

From your example, how will I make it display the results from the query?

My code:

var knex = require('knex')({
  client
: 'mysql',
  connection
: {

    host    
: 'localhost',
    port    
: 3306,
    user    
: 'user1',
    password
: 'password1',
    database
: 'testdb'
 
}
});


knex
.select().table('categories');


I'm trying to display all the items in the table "categories" and then list them as buttons using CSS.  What would be the syntax to display it?  Is it using "console.log"?

Cheers.

Seth.

unread,
Apr 15, 2015, 7:16:03 AM4/15/15
to nwjs-g...@googlegroups.com
knex('categories').select().debug().then(function(res){ // get the result here });

it will show you the query builded by the knex in the console.


--

Joe Goldenwell

unread,
Apr 15, 2015, 9:02:22 AM4/15/15
to nwjs-g...@googlegroups.com
Thanks, but I get:

SyntaxError: Unxepected end of input

:(

Seth.

unread,
Apr 15, 2015, 9:27:56 AM4/15/15
to nwjs-g...@googlegroups.com
knex.select('*').from('categories').debug()
  .then(function(rows) {console.log(rows);})
Try the above code

Seth.

unread,
Apr 15, 2015, 9:29:02 AM4/15/15
to nwjs-g...@googlegroups.com
can you bundle the code and send it to me in case the code i sent you not working.

Joe Goldenwell

unread,
Apr 15, 2015, 9:45:46 AM4/15/15
to nwjs-g...@googlegroups.com
I've managed to get it to display the categories now, but it is also showing the "id" and "category" column titles:

var knex = require('knex')({
  client
: 'mysql',
  connection
: {

    host    
: 'localhost',
    port    
: 3306,
    user    
: 'user1',
    password
: 'password1',
    database
: 'testdb'
 
}
});



knex
.select('*').from('categories').debug()
 
.then(function(rows) {console.log(rows);})

The out put is as follows:

{ __cid: '__cid1',
  method
: 'select',
  options
: undefined,
  bindings
: [],
  sql
: 'select * from `categories`' }
[ { id: 1, catName: 'Category 1' },
 
{ id: 2, catName: 'Category 2' },
 
{ id: 3, catName: 'Category 3' },
 
{ id: 4, catName: 'Category 4' },
 
{ id: 5, catName: 'Category 5' },

I just want it to output Category 1 Category 2 Category 3 etc etc.

Also, the connection doesn't quit.  It just stays in there until I force close it.

Cheers Seth!

Joe Goldenwell

unread,
Apr 15, 2015, 9:57:48 AM4/15/15
to nwjs-g...@googlegroups.com
I've managed to end the connection using:

.finally(function() { knex.destroy(); })

But I can't strip away the "id","#" and "catName" bits.

Seth.

unread,
Apr 15, 2015, 10:23:56 AM4/15/15
to nwjs-g...@googlegroups.com
The output you sent was little wrong as array bracket start and didnt close. Could you sent the the table schema with basic data so i could send you the exact codE

-Seth
--
You received this message because you are subscribed to the Google Groups "nw.js" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nwjs-general...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Joe Goldenwell

unread,
Apr 15, 2015, 10:47:16 AM4/15/15
to nwjs-g...@googlegroups.com
Datbase: testdb
Table: categories
Data:

id   catName
1    Category 1
2    Category 2
3    Category 3
4    Category 4
5    Category 5

Thanks Seth! 

Joe Goldenwell

unread,
Apr 15, 2015, 12:47:34 PM4/15/15
to nwjs-g...@googlegroups.com
I managed to get it done Seth.

My code:

var knex = require('knex')({
  client
: 'mysql',
  connection
: {


    host    
: 'localhost',
    port    
: 3306,
    user    
: 'user1',
    password
: 'password1',
    database
: 'testdb'
 
}
});


knex
.select('*').from('categories').debug()
 
.then(function(rows) {

 
for(var ix in rows){
      console
.log(rows[ix].catName);
}
})


.finally(function() {
  knex
.destroy();
})

Just need to figure out how to use CSS to format the results into buttons.  :P

Seth.

unread,
Apr 15, 2015, 1:05:32 PM4/15/15
to nwjs-g...@googlegroups.com
var str = "";
for(var ix in rows){
//      console
.log(rows[ix].catName);
str += "<button id='"+ix+"'>"+rows[ix].catName+"</button>";
}
document.body.innerHTML = str;

thats the most basic code which can give you the output

--

Raul MATEi

unread,
Apr 15, 2015, 1:27:06 PM4/15/15
to nwjs-g...@googlegroups.com
Hi,

I'm using Sequelize which is built on top of Knex, the rows param in 'then' callback should be an Array, so you can iterate over it like so:

knex
  .select('*')
  .from('categories')
  .then(function (rows) {
     rows.forEach(function (row) {
        // here you can do whatever you want with the data
        console.log(row.catName);
     });
  });

Raul.


--

Joe Goldenwell

unread,
Apr 16, 2015, 5:53:11 AM4/16/15
to nwjs-g...@googlegroups.com
Hi Seth,

I keep getting "Unhandled rejection":

var knex = require('knex')({
  client
: 'mysql',
  connection
: {


    host    
: 'localhost',
    port    
: 3306,
    user    
: 'user1',
    password
: 'password1',
    database
: 'testdb
  }
});


knex.select('
*').from('categories
').debug()
  .then(function(rows) {

  var str = "";
  for(var ix in rows){
    str += "<button id='"+ix+"'>"+rows[ix].catName+"</button>";
}
document.body.innerHTML = str;  


})


.finally(function() {
  knex.destroy();
})

Where have I gone wrong?  Also, how do I use the HTML to take the results and show on the screen?

Joe Goldenwell

unread,
Apr 16, 2015, 5:54:55 AM4/16/15
to nwjs-g...@googlegroups.com
Thanks Raul,

I'll have a look at Sequelize if I can't get this to work.

Seth.

unread,
Apr 16, 2015, 8:29:47 AM4/16/15
to nwjs-g...@googlegroups.com
Am not sure why you are getting unhandled rejection, its the error form bluebird. anyways can you just comment out the for loop and test if you stop getting the error.


--

Joe Goldenwell

unread,
Apr 16, 2015, 9:38:38 AM4/16/15
to nwjs-g...@googlegroups.com
After commenting our the 'for' loop, I get this error:

document.body.innerHTML = str;

SyntaxError: Unexpected identifier


:(

Seth.

unread,
Apr 16, 2015, 11:06:46 AM4/16/15
to nwjs-g...@googlegroups.com
Can you upload the compelte build at any server or share dropbox link so i can make that working

--

Joe Goldenwell

unread,
Apr 17, 2015, 11:25:10 AM4/17/15
to nwjs-g...@googlegroups.com
Cool. Thanks Seth, but which files do you need Seth?  I'm running the mysql database locally.  Do u need that as well?

Seth.

unread,
Apr 17, 2015, 11:47:36 AM4/17/15
to nwjs-g...@googlegroups.com
No DB required, jsut send me the build.

On Fri, Apr 17, 2015 at 8:55 PM, Joe Goldenwell <joegol...@gmail.com> wrote:
Cool. Thanks Seth, but which files do you need Seth?  I'm running the mysql database locally.  Do u need that as well?

--

joejoe...@gmail.com

unread,
Apr 28, 2015, 7:26:56 AM4/28/15
to nwjs-g...@googlegroups.com
Hi Seth,

Sorry for the late response.  I was away for two weeks in Thailand and just got back last night.

I've put the files on to my Dropbox and emailed you the link.  Please let me know if those are the files needed.  The basic structure of the database is:
Thanks again!

Seth.

unread,
Apr 28, 2015, 7:41:21 AM4/28/15
to nwjs-g...@googlegroups.com
Hello Joe,

received, thanks for files, let i make changes and send you

--

Seth.

unread,
Apr 28, 2015, 7:50:29 AM4/28/15
to nwjs-g...@googlegroups.com, joejoe...@gmail.com
Hello Joe,
please find the attached screenshot i think you need this only.

Inline image 1

i am attaching the html file in zip as well.

-Seth


PFA
test.zip

joejoe...@gmail.com

unread,
Apr 28, 2015, 9:21:14 AM4/28/15
to nwjs-g...@googlegroups.com, joejoe...@gmail.com
That was fast Seth!!!  Thank you so much!!

To make the button click to a link, how will do I add "a href" to it?

I've tried:

str += "<a href="'+rows[ix].catURL'"><button id='"+ix+"'>"+rows[ix].catName+"</button></a>";

and

str += "<button id='"+ix+"' onclick="window.location.href='"'+rows[ix].catURL'"'">"+rows[ix].catName+"</button>";


But both don't work.  :(

Basically, I want to be able to style the button using Bootstrap.  But trying to get the basic functionality working first.

Joe Goldenwell

unread,
Apr 28, 2015, 9:38:53 AM4/28/15
to nwjs-g...@googlegroups.com, joejoe...@gmail.com
*UPDATE*

Managed to get the syntax correct now:

str += "<a href='"+rows[ix].catURL+"'><button id='"+ix+"'>"+rows[ix].catName+"</button></a>";

But would like to be able to style them with bootstrap.

Seth.

unread,
Apr 28, 2015, 11:31:41 AM4/28/15
to nwjs-g...@googlegroups.com, joejoe...@gmail.com
See how to add bootstrap class on a ancher tag 
--
You received this message because you are subscribed to the Google Groups "nw.js" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nwjs-general...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Joe Goldenwell

unread,
Apr 28, 2015, 12:14:01 PM4/28/15
to nwjs-g...@googlegroups.com, joejoe...@gmail.com
Got it Seth!  Cheers!

It was simply:

str += "<a href='"+rows[ix].classBtn+"' class='btn btn-lg btn-info'><id='"+ix+"'>"+rows[ix].catName+"</id></a>";


Reply all
Reply to author
Forward
0 new messages