0

I'm new with SQLite and Node.js and so far the application is able to send some data to the front-end. The problem is that it sends only the first row from the table and it should send all the data.

This is the code so far:

var express = require('express');
var router = express.Router();
var sqlite3 = require('sqlite3').verbose();
var db = new sqlite3.Database('./db/my_db.db');

router.get('/', function (req, res, next) {
  db.serialize(function () {
    db.each('SELECT id, name, surname FROM Customers', function (
      err,
      row
    ) {
      res.send(row);
    });
  });

  db.close();
});

module.exports = router;

It seems that isn't enough to insert res.send() inside db.each() because it does the sending only for the first column.

How can it be done for all the data from that table?

1 Answer 1

3

The problem is related to db.each() and res.send() because the later one is called only once, sends the response and closes, doesn't go in a loop for all the rows.

The simplest solution in this case is to use db.all() instead.

var express = require('express');
var router = express.Router();
var sqlite3 = require('sqlite3').verbose();
var db = new sqlite3.Database('./db/my_db.db');

router.get('/', function (req, res, next) {
  db.serialize(function () {
    db.all('SELECT id, name, surname FROM Customers', function (
      err,
      row
    ) {
      res.send(row);
    });
  });

  db.close();
});

module.exports = router;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.