0

我在我的数据库中存储一个函数并使用 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();}
4

3 回答 3

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();
 })
于 2013-02-28T15:23:18.190 回答
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)
于 2013-02-28T15:18:48.597 回答
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();

您可能在“设置”之前忘记了一点

于 2013-02-28T15:30:15.547 回答