0
I’m creating a API to then be consumed in React. In this API you will have a route(page) that will list database users.
I applied the rules and I’m testing in India, which by the way is working well.
My problem and when I leave the parameter of url emptiness, Example: localhost/user/.
I wish that when left empty returns the first page.
follows the code below:
if(results){
const numero_registros = results.length; // Existe 37 registros no banco
let pagina_atual = req.params.page || 1;
let total_paginas = Math.ceil(numero_registros/10);
let count = (pagina_atual*10)-10;
connection.query(`SELECT * FROM usuarios_homos LIMIT 10 OFFSET ${count}`,(err,result,fields)=>{
if(err){
console.log(err);
}else{
res.send(result);
}
});
Screenshot of the Insomnia showing the page:
Image of the beginning when I don’t pass the parameter
[PROBLEM SOLVED]
// Páginação
if(results){
const rows = results.length; // Existe 37 registros no banco
let { page = 1 } = req.query; // passando argumento pela url >> usuarios?page=1
let calc = Math.ceil(rows/10); // calc recebe a quantidade de páginas existentes
if(page == ''){
page = 1;
}
let count = (page*10)-10;
const qtd = connection.query(`SELECT * FROM usuarios_homos LIMIT 10 OFFSET ${count}`,(err,result,fields)=>{
if(err){
console.log(err);
}else{
res.send(result);
}
});
ON THE ROUTES BECAME:
router.get('/usuarios',queryController);
What happens when you do not pass the page by parameter in the url?
– Rafael Tavares
Return me in India 404 Not Found
– Gabriel Mariano
As you stated the route in Node.js?
– Rafael Tavares
Put >> router.get( '/usuarios/:page', queryController);
– Gabriel Mariano
Although the question has already been resolved, I recommend editing it to add the code of the route statement. It is important and can help people with the same problem in the future.
– Rafael Tavares