Armand
Armand

Reputation: 10264

Unexpected token ) node js eval a functtion

I am storing a function in my database and retrieving it from the database with node.js

When I console.log the column containing the function this is the output

(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();})

When I console.log typeof column it prints out string

But when I do

var func = eval(column);

It results in Unexpected token )

Does anyone know why?

I have made my function smaller now:

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();}

Upvotes: 0

Views: 1186

Answers (3)

nialna2
nialna2

Reputation: 2224

Your JSON probably has problems. You should write it using multiple lines :

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();
}

As you can see now, there is a problem here :

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

You probably forgot a point before "settings"

Upvotes: 1

ygaradon
ygaradon

Reputation: 2298

(
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();
 })

Upvotes: 2

Floby
Floby

Reputation: 2336

Have you considered the possibility that there might be an extraneous or misplaced closing parenthesis ? this is what node.js outputs when fed what you included in you question

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)

Upvotes: 1

Related Questions