0

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 ('[email protected]','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 Answer 1

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)
Sign up to request clarification or add additional context in comments.

1 Comment

Yep, that's the one - except currently I'm at the server-side only stage, so once the variables have been defined it's: var insertUser = edge.func('sql', function () {/* INSERT INTO dbo.Newsletter (Email, Active, DateCreated, DateUpdated, Deleted) VALUES (@email,@active,@dateCreated,@dateUpdated,@deleted) */}); insertUser({ email: emailAddress, active: 1, dateCreated: strDateTime, dateUpdated: strDateTime, deleted: 0 }, function (){code});

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.