0

Evenings,

Im trying to make a simple CRUD, adn I made the GET, so i guess de DB connection is OK, but when I try to make the POST, it throws me an error, 46201. I tried making the query in pgAdmin and it works (insert values in table), idk if its something with "quotes"

app.post('/books', (req, res) => {
    const query = "INSERT INTO books (`title`,`description`,`coverimg`,`price`) VALUES ('$1,$2,$3,44);"
    const title = "title_from_back";
    const description = "back_desc";
    const coverimg = "cover_pic_back";
    const price = '33';
    client.query(query, [ title, description, coverimg ],(err, data) => {
        if (err) return res.json(err);
        return res.json(data)
    })
})

I tried changing the price for integer.

when I launch the crud using insomnia it response me an error:

{
    "length": 90,
    "name": "error",
    "severity": "ERROR",
    "code": "42601",
    "position": "20",
    "file": "scan.l",
    "line": "1145",
    "routine": "scanner_yyerror"
}

Thanks for helping.

Juan

Dai
  • 141,631
  • 28
  • 261
  • 374

1 Answers1

0

My first guess is that you're missing price in your parameters. Also, 44 instead of $4. Also, single unmatched quote. Revised below:

app.post('/books', (req, res) => {
    const query = "INSERT INTO books (`title`,`description`,`coverimg`,`price`) VALUES ($1,$2,$3,$4);"
    const title = "title_from_back";
    const description = "back_desc";
    const coverimg = "cover_pic_back";
    const price = '33';
    client.query(query, [ title, description, coverimg, price ],(err, data) => {
        if (err) return res.json(err);
        return res.json(data)
    })
})

Don't know if this will fix it, but for sure handles the obvious.

Yimin Rong
  • 1,890
  • 4
  • 31
  • 48
  • Thanks for trying to help, but this is not the problem, I wrote it in my code, just passed when I wrote here. – orozco746 Sep 30 '22 at 22:15