NodeJS + sqlite3: error getting information from Select

2

I'm learning nodeJS.

I'm trying to integrate it into Sqlite3, but when trying to read a database information the following error is being generated:

/home/ubuntu/workspace/node_modules/sqlite3/lib/trace.js:28                                                                                          
                    throw err;                                                                                                                       
                          ^                                                                                                                          
TypeError: Cannot read property 'counter' of undefined                                                                                               
    at Statement.<anonymous> (/home/ubuntu/workspace/server.js:15:56)                                                                                
--> in Database#each('SELECT COUNT(id) AS counter FROM tasks', [Function])                                                                           
    at Database.<anonymous> (/home/ubuntu/workspace/server.js:10:10)                                                                                 
    at Object.<anonymous> (/home/ubuntu/workspace/server.js:6:6)                                                                                     
    at Module._compile (module.js:456:26)                                                                                                            
    at Object.Module._extensions..js (module.js:474:10)                                                                                              
    at Module.load (module.js:356:32)                                                                                                                
    at Function.Module._load (module.js:312:12)                                                                                                      
    at Module.runMain [as _onTimeout] (module.js:497:10)                                                                                             
    at Timer.listOnTimeout [as ontimeout] (timers.js:110:15)

The following is the server code:

    var fs = require('fs');
    var sqlite3 = require('sqlite3').verbose();
    var db = new sqlite3.Database("teste_db");

    db.serialize(function() {
        db.run("CREATE TABLE IF NOT EXISTS tasks (id INTEGER PRIMARY KEY, expires INTEGER, done SMALLINT, user CHARACTER(32))");
        db.each('SELECT COUNT(id) AS counter FROM tasks', function(err, rows){
                if (err) {
                    callback(err);
                    return;
                }
                console.log("contagem de linhas: " + rows[0].counter);
            });
        });
    db.close();

No matter what name I give the field to be read in the database, it always assumes that the field name is a property of the ROWS array.

NOTE: In the example I used a COUNT, but in any type of select it returns this error

Can anyone tell me where I'm wrong?

    
asked by anonymous 25.07.2014 / 16:40

1 answer

1

The COUNT group function does not return an array. If you inspect the return you will check the following JSON: { counter: 0 } . Therefore, modify the line where you print the row count to

console.log("contagem de linhas: " + rows.counter);

and everything will work correctly.

Q.: Since I do not actually return multiple records, I suggest changing rows to row , result or something more appropriate.

    
28.07.2014 / 14:20