Node.js / Edge.js - 将JS变量插入SQL [英] Node.js / Edge.js - Insert JS variables into SQL

查看:148
本文介绍了Node.js / Edge.js - 将JS变量插入SQL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用带有Edge.js的Node.js将硬编码值插入SQL数据库:

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')
*/});

这是正常的。

但是,我希望这些值是先前定义的JS变量,而不是硬编码。所以如果我定义了这个变量:

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')
*/});

以上语法(以及各种变体)不起作用 - 是否可以插入带有Node / Edge的SQL中的JS变量?非常感谢提前...

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...

推荐答案

您可以在函数中使用命名参数,如下所示:

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)

这篇关于Node.js / Edge.js - 将JS变量插入SQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆