Home > OS >  Node js SyntaxError: Unexpected identifier SQL update
Node js SyntaxError: Unexpected identifier SQL update

Time:02-16

I am following this Youtube video https://www.youtube.com/watch?v=YYEC7ydDj4k learning the basics of node js with MySQL and I have typed the coded exactly as the video but I am getting the following error screenshot

let sql = 'UPDATE employee SET name = '${newName}' WHERE id = ${req.params.id}' ^

SyntaxError: Unexpected identifier at Object.compileFunction (node:vm:352:18) at wrapSafe (node:internal/modules/cjs/loader:1026:15) at Module._compile (node:internal/modules/cjs/loader:1061:27) at Object.Module._extensions..js (node:internal/modules/cjs/loader:1151:10) at Module.load (node:internal/modules/cjs/loader:975:32) at Function.Module._load (node:internal/modules/cjs/loader:822:12) at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:77:12) at node:internal/main/run_main_module:17:47

Node.js v17.5.0

//update employee
app.get('/updateemployee/:id', (req,res) => {
    let newName = 'Updated name'
    let sql = 'UPDATE employee SET name = '${newName}' WHERE id = ${req.params.id}'
    let query = db.query(sql,err => {
        if(err {
            throw err
        }
        res.send('Employee Updated')
    })
})

Any help would be highly appreciated! Thank you!

CodePudding user response:

To use variable substitution inside strings you need to use the backtick character ` and not quotes.

let sql = `UPDATE employee SET name = '${newName}' WHERE id = ${req.params.id}` 
  • Related