我目前正在将Edge.js与Node.js结合使用,以将硬编码值插入SQL数据库中:
var insertUser = edge.func('sql', function () {/* INSERT INTO dbo.Newsletter (Email, Active, DateCreated, DateUpdated, Deleted) VALUES ('test2@test.com','1','2014-01-01 00:00:00','2014-01-01 00:00:00','0') */});
这可以正常工作。
但是,我希望这些值是以前定义的JS变量,而不是硬编码的。因此,如果我定义了此变量:
var emailAddress = document.getElementById('emailInput').value;
我想将其插入硬编码的电子邮件地址(下面的问题代码):
var insertUser = edge.func('sql', function () {/* INSERT INTO dbo.Newsletter (Email, Active, DateCreated, DateUpdated, Deleted) VALUES ('emailAddress','1','2014-01-01 00:00:00','2014-01-01 00:00:00','0') */});
上面的语法(及其各种变体)不起作用-是否可以在带有Node / Edge的SQL中插入JS变量?提前谢谢了…
您可以在函数中使用命名参数,如下所示:
var insertUser = edge.func('sql', function () {/* INSERT INTO dbo.Newsletter (Email, Active, DateCreated, DateUpdated, Deleted) VALUES (@emailAddress,'1','2014-01-01 00:00:00','2014-01-01 00:00:00','0') */});
向其传递具有如下参数的对象:
var emailAddress = document.getElementById('emailInput').value; insertUser({"emailAddress": emailAddress}, callBack)