[英]How to covert JavaScript string to executable code in node.js
I have saved query in database. 我已将查询保存在数据库中。 I can get query as a string.
我可以查询字符串。
INSERT INTO alertsV SET Instance_ID = ${body.add.Instance_ID}
But ${body.add.Instance_ID}
is not replacing with value. 但是
${body.add.Instance_ID}
不会替换为值。
expected result should be INSERT INTO alertsV SET Instance_ID = 1234
预期结果应为
INSERT INTO alertsV SET Instance_ID = 1234
I am using nodejs. 我正在使用nodejs。 Please help me how to fix this.
请帮助我解决此问题。 Here is the function.
这是功能。
saveFormData: async function (req, res) {
let form_id = parseInt(req.body.form_id);
let body = req.body.form_data;
let saveQuery = await sails.orientDb.command('select actions.save as
saveQuery from form where form_id=' + form_id).one();
saveQuery = saveQuery.saveQuery; // i can get string here 'INSERT INTO alertsV SET Instance_ID = ${body.add.Instance_ID}'
let result = await sails.orientDb.command(saveQuery).one();
return res.json(result);
}
You can wrap a string in the template literal backticks and pass it to eval
to run string interpolation: 您可以将字符串包装在模板文字反引号中,并将其传递给
eval
以运行字符串插值:
saveQuery = eval('`' + saveQuery.saveQuery + '`');
const a = 10; const str = 'test ${a} test'; console.log(eval('`' + str + '`')); /* if it's required to wrap the interpolated value into a quotes, you can do that by wrapping the embedded expression, prior to interpolation: */ console.log(eval('`' + str.replace(/(\\${.*})/, '\\'$1\\'') + '`'));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.