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

345
Visualizações
Insert function in node in postgre

Hello i am following a tutorial and trying to do a simple insert query but getting 404 error.I am trying to input values using postman.

function

function insertUser(req, res, next){
  req.body.users = parseInt(req.body.users);
  
  db.none('INSERT INTO office.users (role_id, office_id, user_name, full_name) ' +
    'VALUES (${role_id}, ${office_id}, ${user_name}, ${full_name})', req.body.users)
   .then(function(){
        res.status(200)
        .json({
          status: 'success',
          message: 'Inserted one user'
   });
   })
 .catch(error => {
        console.log(error);
        next();
})
}

The stack trace says problem is near "values".i am not getting it.following i flow with the documentation.Something wrong with the query?

Stack trace

POST /api/users 401 83.997 ms - 43 { [error: syntax error at or near "$"] name: 'error', length: 102, severity: 'ERROR', code: '42601', detail: undefined, hint: undefined, position: '78', internalPosition: undefined, internalQuery: undefined, where: undefined, schema: undefined, table: undefined, column: undefined, dataType: undefined, constraint: undefined, file: 'src\backend\parser\scan.l', line: '1053', routine: 'scanner_yyerror' }

I am on my 5th day with Node.Bear with me.

about 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

Error code 42601 is a postgres syntax error. If you were to print our your sql statement to the page you would see something like this:

"INSERT INTO office.users VALUES (role_id,office_id,user_name,full_name)values(${role_id},${office_id},${user_name},${full_name})"

There's multiple things wrong with this. First, your preceding your list of columns with VALUES. Second, you have no space between your list of columns and the (proper) values key word. Your statement should look like this:

"INSERT INTO office.users (role_id, office_id, user_name, full_name) VALUES (${role_id}, ${office_id}, ${user_name}, ${full_name})"

Which means your javascript needs to look more like this:

db.none('INSERT INTO office.users (role_id, office_id, user_name, full_name) ' +
        'VALUES (${role_id}, ${office_id}, ${user_name}, ${full_name})', req.body.users);
about 4 years ago · Santiago Trujillo Relatório

0

Silly mistake declaring variable

This worked

 db.none('INSERT INTO office.users(role_id, office_id, user_name, full_name, password)' +
         'values(${role_id},${office_id},${user_name},${full_name},${password})', req.body)

about 4 years ago · Santiago Trujillo 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