Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
312 views
in Technique[技术] by (71.8m points)

sql - Javascript PostgreSQL request WHERE id LIKE $1, [ element + '%']

I would like to be able to see for example all the id that start with 1 (1, 11, 12, 100 etc ...) :

static async adminFindOne(element) {
    const user = await db.query(`
        SELECT * FROM "user" 
        WHERE id LIKE $1;`, [element + '%' ]
    );
    // console.log(user.rows[0]);
    return user.rows;
};

when i try it on my server, it returns me :

Cet utilisateur n'existe pas. error: syntax error at or near "$1"
at Parser.parseErrorMessage (/home/ubuntu/projet-diy/api/node_modules/pg-protocol/dist/parser.js:278:15)
at Parser.handlePacket (/home/ubuntu/projet-diy/api/node_modules/pg-protocol/dist/parser.js:126:29)
at Parser.parse (/home/ubuntu/projet-diy/api/node_modules/pg-protocol/dist/parser.js:39:38)
at Socket.<anonymous> (/home/ubuntu/projet-diy/api/node_modules/pg-protocol/dist/index.js:10:42)
at Socket.emit (node:events:376:20)
at addChunk (node:internal/streams/readable:311:12)
at readableAddChunk (node:internal/streams/readable:286:9)
at Socket.Readable.push (node:internal/streams/readable:225:10)
at TCP.onStreamRead (node:internal/stream_base_commons:192:23) {


length: 91,
  severity: 'ERROR',
  code: '42601',
  detail: undefined,
  hint: undefined,
  position: '60',
  internalPosition: undefined,
  internalQuery: undefined,
  where: undefined,
  schema: undefined,
  table: undefined,
  column: undefined,
  dataType: undefined,
  constraint: undefined,
  file: 'scan.l',
  line: '1127',
  routine: 'scanner_yyerror'
}

The problem seems not exist when its not a INTEGER, i can do it with an email, or nickname

If someone have a solution ... :)

Thanks for your helping

question from:https://stackoverflow.com/questions/66054780/javascript-postgresql-request-where-id-like-1-element

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...