Node.js / Edge.js - Insert JS variables into SQL

2019-06-14 13:54发布

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条回答
地球回转人心会变
2楼-- · 2019-06-14 14:47

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)
查看更多
登录 后发表回答