Reputation: 6186
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?
Upvotes: 0
Views: 1057
Reputation: 6186
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;
Upvotes: 3