I'm trying to run a simple website, and encountered an following backtick error
`INSERT INTO questions(qid, uid, question, difficulty, cid) VALUES(${qid},${uid},${question},${difficulty},${cid})`,
^^^^^^
SyntaxError: Unexpected identifier
at Object.exports.runInThisContext (vm.js:78:16)
at Module._compile (module.js:543:28)
at Object.Module._extensions..js (module.js:580:10)
at Module.load (module.js:488:32)
at tryModuleLoad (module.js:447:12)
at Function.Module._load (module.js:439:3)
at Module.runMain (module.js:605:10)
at run (bootstrap_node.js:420:7)
at startup (bootstrap_node.js:139:9)
at bootstrap_node.js:535:3
and here's the code
app.put('/problems', function(req, res) {
pool.getConnection(function(err, connection) {
var p_list = new Array(4);
var qid = mysql.escape(req.body.qid);
var uid = mysql.escape(req.body.uid);
var question = mysql.escape(req.body.question);
var difficulty = mysql.escape(req.body.difficulty);
var cid = mysql.escape(req.body.cid);
var choices = req.body.choices;
var answer = mysql.escape(req.body.answer);
var explanation = mysql.escape(req.body.explanation);
var qid_choice = ``;
choices.forEach( choice => {
choice = mysql.escape(choice);
qid_choice += "("+qid+", "+choice+"),";
} );
qid_choice = qid_choice.slice(0,-1);
var queries = [
`INSERT INTO questions(qid, uid, question, difficulty, cid) VALUES(${qid},${uid},${question},${difficulty},${cid})`,
`INSERT INTO questionInfo(qid) VALUES(${qid})`,
`INSERT INTO choices(qid, choice) VALUES ${qid_choice}`,
`INSERT INTO solutions(qid, answer, explanation) VALUES(${qid},${answer},${explanation})`
];
for (let i=0; i<4; i++) {
p_list[i] = new Promise(function(resolve, reject) {
connection.query(
queries[i],
err => {
if (err) reject(err);
else resolve();
}
);
});
}
Promise.all(p_list).then(function() {
connection.release();
console.log(`[200] ${req.method} to ${req.url}`);
res.end();
}, function(err) {
connection.release();
console.log(`[500] ${req.method} to ${req.url} because ${err}`);
})
});
});
I'm using node version 7.3.0
I have no idea why this error occurred... It's too frustrating
Thank you for reading :)
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…