Home > other >  MySQL where clause in node.js
MySQL where clause in node.js

Time:01-13

How to pass id in where clause using node.js and MySQL

Viewer.jsx

async function redeem(cid) {
  fetch('http://localhost:4000/getDetailsFromCid/${cid}').then(response => {
    return response.json()
  })
    .then(posts => {
      console.log("posts", posts)    

    })
    .then((err) => {
      console.log(err);
    })
}

index.js

app.get("/api/getDetailsFromCid/:cid", (req, res) => {
    const cid = req.params.cid;
    db.query("SELECT * FROM Details WHERE cid = ?", [cid],
        (err, result) => {
            if (err) {
                console.log(err)
            }
            res.send(result)
        });
});

Error

Viewer.jsx:19          GET http://localhost:4000/getDetailsFromCid/${cid} 404 (Not Found)

CodePudding user response:

you need to use `` not ''

fetch(`http://localhost:4000/getDetailsFromCid/${cid}`)
  • Related