问题
I'm currently using Node.js with Edge.js to insert hardcoded values into a SQL database:
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')
*/});
This works correctly.
However, I'd like the values to be previously-defined JS variables, not hardcoded. So if I have defined this variable:
var emailAddress = document.getElementById('emailInput').value;
I would like to insert it in place of the hardcoded email address (problem code below):
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')
*/});
The above syntax (and various variations of) doesn't work - is it possible at all to insert JS variables in SQL with Node/Edge? Many thanks in advance...
回答1:
You can use named parameters in your function like this:
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')
*/});
Pass it an object with parameters like this:
var emailAddress = document.getElementById('emailInput').value;
insertUser({"emailAddress": emailAddress}, callBack)
来源:https://stackoverflow.com/questions/22831936/node-js-edge-js-insert-js-variables-into-sql