Nodejs + sqlite3: error retrieving information from Select

Asked

Viewed 166 times

2

I’m learning nodes.

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

/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)

Below 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 to 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 someone tell me where I’m going wrong?

  • can log the Rows object and post here? can be that the table has not been created, by the asynchronous nature of the nodejs

  • Another thing, there is a very light and simple ORM for Ode, with great documentation. It’s called Sequelize, I recommend taking a look anytime. Makes the job easier

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 line count to

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

and everything will work properly.

P.S.: as they don’t actually return multiple records, I suggest changing rows for row, result or something more appropriate.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.