意外令牌)节点js评估一个函数

时间:2013-02-28 15:14:27

标签: javascript node.js eval

我正在我的数据库中存储一个函数,并使用node.js

从数据库中检索它

当我console.log包含该函数的列时,这是输出

(function(settings){var options = {host: 'somehost.com',path: 'some/path/' + settings.token + '?mobile=' + settings.number + '&message=' + settings.message};callback = (function(response) {var str = '';response.on('data', (function (chunk) {str += chunk;}));response.on('end', (function () {settings.result(str);})));}settings.httpRequest.request(options, callback).end();})

当我console.log typeof列时,它会打印出string

但是当我做的时候

var func = eval(column);

结果为Unexpected token )

有谁知道为什么?

我现在缩小了我的功能:

function(settings){var options = {host: 'api.smsmonster.co.za',path: '/uv1.svc/SendSMS/' + settings.token + '?mobile=' + settings.number + '&message=' + settings.message}settings.httpRequest.request(options, settings.callback).end();}

3 个答案:

答案 0 :(得分:2)

(
function(settings){
 var options = 
  {
     host: 'somehost.com',
     path: 'some/path/' + settings.token + '?mobile=' + settings.number + '&message=' +        settings.message
  };
 callback = ( function(response) {
   var str = '';
   response.on('data', (function (chunk) {
    str += chunk;
    })
);
response.on('end', (function () {
  settings.result(str);
  })
  )
 /*{!here}*/);
} //<-- this is your problem it need to go to:{!}
settings.httpRequest.request(options, callback).end();
 })

答案 1 :(得分:1)

您是否考虑过可能存在无关或错位的右括号? 这就是node.js输入你在问题中包含的内容时输出的内容

chunk;}));response.on('end', (function () {settings.result(str);})));}setting
                                                                    ^
SyntaxError: Unexpected token )
    at Module._compile (module.js:437:25)
    at Object.Module._extensions..js (module.js:467:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Module.runMain (module.js:492:10)
    at process.startup.processNextTick.process._tickCallback (node.js:244:9)

答案 2 :(得分:1)

你的JSON可能有问题。你应该用多行来写它:

function(settings){
  var options = {
   host : 'api.smsmonster.co.za',
   path : '/uv1.svc/SendSMS/' 
          + settings.token 
          + '?mobile=' + settings.number 
          + '&message=' + settings.message
  }settings.httpRequest.request(options, settings.callback).end();
}

如您所见,此处存在问题:

 }settings.httpRequest.request(options, settings.callback).end();

您可能在“设置”

之前忘记了一点