SQL command works on SQL SHELL, but does not work on Node (pg) (probably something related to accentuation)

Asked

Viewed 27 times

-2

DATABASE
I have this database: Banco de dados chamado exemplo In it I have this table:
Tabela pessoas dentro do banco de dados exemplo
In it I have this data:
inserir a descrição da imagem aqui

NODE

const express = require('express');
const app = express();
const cors = require('cors');

// cors config // origin: 'http://127.0.0.1:3000/'
app.use(cors({
  origin: '*'
}));

// middlewares
app.use(express.json());
app.use(express.urlencoded({ extended: false }))

// Database
const { Pool } = require('pg');
const parse = require('pg-connection-string').parse;
var pool = null;

pool = new Pool({
    host: 'localhost',
    user: 'postgres',
    password: 'admin',
    database: 'exemplo',
    port: '5432'
});

// Routes
const { Router } = require('express');
const routes = Router();

routes.get('/pessoas', async function(req, res) {
  const response = await pool.query("select * from pessoas where disciplina = 'Matemática'");
  res.status(200).json({response: response.rows});
},);

app.use(routes);

let port = process.env.PORT;
if (port == null || port == "") {
  port = 3000;
}
app.listen(port);

PROBLEM
When executing, "SELECT * FROM people WHERE discipline = 'Mathematics', this appears: inserir a descrição da imagem aqui
That is, no problem so far worked perfectly. HOWEVER, when opening the route "/people" (Node code above):
inserir a descrição da imagem aqui
no results are returned. But if I choose, for example, "Geography" instead of "Mathematics", that by chance the only difference is acute accent:

routes.get('/pessoas', async function(req, res) {
  const response = await pool.query("SELECT * FROM pessoas WHERE disciplina = 'Geografia'");
  res.status(200).json({response: response.rows});
},);

Look at: inserir a descrição da imagem aqui

END
Please, if you can handle this, help me. I’ve looked for answers in places and tried several things (I changed the CLIENT_ENCODING, I changed the Ctype, I even touched the Collate, etc.). I believe it’s some silly detail I’m not aware of.

2 answers

0

Replace the code snippet below:

routes.get('/pessoas', async function(req, res) {    
 const response = await pool.query("select * from pessoas where disciplina = 'Matemática'");    
 res.status(200).json({response: response.rows}); 
});

This way:

routes.get('/pessoas', async function(req, res) {    
  const response = await pool.query('SELECT * FROM pessoas WHERE disciplina = $1', ['Matemática']);      
  res.status(200).json({response: response.rows});  
});

Important consideration:

Always use queries with parameterized values, that is, do not enter the values directly in the query’s SQL command to avoid SQL Injection attacks.

Link to the documentation of the Node-postgres.

  • True, using queries without parameterizing the values is risky, but this example above is just a simplification of the actual design, in it I took this precaution. My problem has already been solved, I did not understand what happened, but when I deployed to the server the accents worked.

  • Just locally it wasn’t working, but I’m still not sure I’ll perform some tests.

-2


After I installed on the server this problem stopped happening.

Browser other questions tagged

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