2013-02-28 58 views
0

我存儲在我的數據庫的功能,並從與的node.js意外令牌)節點JS的eval一個functtion

數據庫中檢索它,當我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.logtypeof列它打印出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();} 
+0

忘了一點嘗試去掉括號圍繞函數聲明。 – 2013-02-28 15:18:50

回答

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

你有沒有考慮過的可能性,有可能是外來的或錯位右括號? 這是當你喂什麼包括什麼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) 
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(); 

你可能之前的「設置」