When a run the node.js server it’s ok. But when I try run the person GET route, it returns a empty json array and logs: SQLITE_CANTOPEN: unable to open database file.
I don’t undersatand why can’t the database file be oppend.
I have the following .js and .db files on my directory:
The database files:
/api/database/databse.db
/api/database/connection.js
//This is the connection.js
const sqlite3 = require('sqlite3').verbose();
exports.open = (database) => {
database = new sqlite3.Database(database, (err) => {
if (err) {
return console.error(err.message);
}
});
return database;
}
exports.close = (database) => {
database.close((err) => {
if (err) {
return console.error(err);
}
});
}
The Model file:
/api/model/UserModel.js
const database = require('../database/connection');
class Person {
constructor (id, firstName, lastName, birthdate, height, weight) {
this.id = id || 0;
this.firstName = firstName || "none";
this.lastName = lastName || "none";
this.height = height || 0;
this.weight = weight || 0;
this.birthdate = birthdate ? new Date(birthdate) : "none";
}
// [...] getter functions that calculates and concatenates datas
}
// [...] annother databse function, not used yet (register function)
function getEveryone () {
return new Promise((resolve, reject)=>{
let everyone = [];
const sql = "SELECT * FROM person;";
const db = database.open('./database/database.db');
db.all(sql, [], (err, rows) => {
if (err) {
reject(JSON.stringify({message : `Erro: ${err.message}`}));
}
rows.forEach(row => {
const id = row.id;
const firstName = row.firstName;
const lastName = row.lastName;
const height = row.height;
const weight = row.weight;
const birthdate = row.birthdate;
everyone.push(new Person(id, firstName, lastName, height, weight, birthdate));
});
});
database.close(db);
resolve(everyone);
});
}
module.exports = {
Person,
getEveryone,
register
}
The Controller file:
/api/controller/PersonController.js
const Person = require('../model/PersonModel');
async function getEveryone (req, res) {
try {
const everyone = await Person.getEveryone();
console.log(`Everyone on Controller: ${everyone}`);
res.writeHead(200, {'Content-Type': 'application/json'});
res.end(JSON.stringify({everyone}));
} catch (error) {
res.writeHead(500, {'Content-Type': 'application/json'});
res.end(JSON.stringify({"startStats" : `Reporte o ocorrido! Ocorreu o seguinte erro: ${error}`}));
}
}
module.exports = {
getEveryone
}
And the server.js file
/api/server.js
const http = require('http');
const { getEveryone } = require('./controller/PersonController');
const hostname = '127.0.0.1';
const port = 3000;
const server = http.createServer((req, res)=>{
const url = req.url;
if (req.method == 'GET') {
switch (url) {
case "/api/person":
getEveryone(req, res);
break;
}
} else if (req.method == 'POST') {
switch (url) {
case "/api/person":
res.writeHead(201, {'Content-Type': 'application/json'});
res.end(JSON.stringify({"startStats" : "Cadastro realizado com sucesso!"}));
break;
}
} else {
res.writeHead(404, {'Content-Type': 'application/json'});
res.end(JSON.stringify({"startStats" : "Rota não encontrada!"}));
}
});
server.listen(port, hostname, ()=>{
console.log(`Servidor rodando em http://${hostname}:${port}/`);
});
On this part of the API, I want to get all the table person’s lines.
I have already tried to change the database path, on PersonModel.js for '../database/database.db' or './../database/database.db'. But I had even gotten the same error.
So first, I want to open the databse file and then see if the API is querying right.