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

荒凉一梦 提交于 2019-12-10 12:04:18

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!