Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

119
Visualizações
How do I use var into query request in javascript?

Hello i'm struggling with using variables that I declared into my jquery request:

var int_example = "42";
int_example = parseInt(id);
var string_example = "42";

const { Client } = require('pg')
const client = new Client({
    user: 'postgres',
    host: 'localhost',
    database: 'postgres',
    password: 'password',
    port: 5432
});

var query = "INSERT INTO data (int,string) VALUES (int_example,string_example);
client.connect()
    .then(() => console.log("Connection a la base de données réussie"))
    .then(() => console.log("Nos commandes:"))
    .catch(e => console.log(e))
    .then(() => client.query(query))

I get this kind of error : "{ error: column "int_example" does not exist" and i'm not sure if the string_example works as well... Seems I can't use type var or i'm doing something wrong please tell me.

Thank you

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

there are two ways to put variables in JS strings:

  1. concatenation
var query = "INSERT INTO data (int,string) VALUES (" + int_example + ",'" + string_example + "');";
  1. using template literals (recommended)
var query = `INSERT INTO data (int,string) VALUES (${int_example},'${string_example}');`

right now your query does not contain values of int_example and string_example and it's just their names.

about 4 years ago · Juan Pablo Isaza Relatório

0

Your issues results from your query string. The values of int_example and string_example are not present in your query.

You can fix the issue by either using string interpolation or string concatenation

You solution will be to use one of the below options:

String Interpolation:

var query = `INSERT INTO data (int,string) VALUES (${int_example},${string_example})`;

String Concatenation:

var query = "INSERT INTO data (int,string) VALUES (" + int_example + "," + string_example + ")";

Note: Whenever you create a string from a string and a number/int the result is a string. Number/int is converted into a string when concatenating.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda