Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Node js SyntaxError: Unexpected identifier SQL update

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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!

>Solution :

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}` 
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading