2

How come eval does not work under nodejs?

function show(myVariable) {
    var myValue = eval(myVariable);
    console.log(myVariable + "=" + myValue);
}

var foo = "bar";
show("foo");

on a HTML page this returns foo=bar in the console log

in a nodejs controller it gives SyntaxError: Invalid or unexpected token

This is the original code:

"use strict";
var mongoose = require('mongoose');

function show(myVariable) {
    var myValue = eval(myVariable);
    console.log(myVariable + "=" + myValue);
}

exports.messageSave = function(req, callback) {

    var foo = "bar";
    show("foo");

    return

    // a lot of other code below, but put return to focus on the problem

}

UPDATE

What I am trying to do here is for my show function to read the name of the variable that was parsed to it.

torbenrudgaard
  • 2,375
  • 7
  • 32
  • 53
  • eval is bold!! Count yourself lucky! But I will refer you to here https://stackoverflow.com/a/10491365/1606432 – Pogrindis Jun 27 '17 at 22:54
  • 1
    `eval` does evaluate the expression in global scope, but unlike in the browser that `foo` variable is not a global in node – Bergi Jun 27 '17 at 23:06
  • @Pogrindis nope, use strict made no difference. – torbenrudgaard Jun 27 '17 at 23:08
  • @Bergi ahhhh!! so as soon as you have some `exports.messageSave = function(req, callback) {` controllers, then it is no longer in the global scope and it will no longer work. Is there another way to know the name of the variable that calls a function? – torbenrudgaard Jun 27 '17 at 23:10
  • @torb I was not suggesting you to use strict! I was putting you off eval entirety! :D – Pogrindis Jun 27 '17 at 23:11
  • @Pogrindis ok the link you sent suggested using strict :) – torbenrudgaard Jun 27 '17 at 23:13
  • Hmmm if only there was another way for my function to see the name of the variable that called it. This is what I need - would rather avoid using eval. – torbenrudgaard Jun 27 '17 at 23:14
  • Oh, wait, wait, maybe I was wrong. Your `eval` is a direct call and should find all variables in scope, even the local ones. What's really weird though is that `SyntaxError: Invalid or unexpected token` - `foo` is a perfectly valid expression statement however. Can you show us the actual code you are using? – Bergi Jun 27 '17 at 23:15
  • Not sure what you mean by "*my function to see the name of the variable that called it*". Are you looking for a stack trace? Maybe you should ask a new question and state your [actual problem](https://meta.stackexchange.com/q/66377) so that we might suggest an `eval`-less solution. – Bergi Jun 27 '17 at 23:16
  • @Bergi sorry, I updated the question with my original code and tried to be more specific with the actual problem. And what is a stack trace? Is that something I could use? – torbenrudgaard Jun 27 '17 at 23:21
  • 1
    Yes, *that* `show` function does definitely not have access to the scope that `foo` is defined in. It really would be much simpler to pass name and value (`show("foo", foo)`) or an object (`show({foo})`). – Bergi Jun 27 '17 at 23:23
  • @torbenrudgaard A [stack trace](https://en.wikipedia.org/wiki/Stack_trace) can be accessed as [`new Error().stack`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error/Stack) – Bergi Jun 27 '17 at 23:24
  • @Bergi you are right - changing my show function is probably the best way, otherwise it will be too much hackery :) And thanks for showing me the error.stack, didnt know about that one. Please write a short answer so I can close the question :) – torbenrudgaard Jun 27 '17 at 23:27

1 Answers1

1

Your show function does not work because it does not have access to the scope that foo is defined in. It would only work with global variables, which is probably what you tried in the browser.

Don't do too much trickery, and don't use eval for it. It really would be much simpler to pass name and value (show("foo", foo)) or an object (show({foo}) - ES6 property shorthand syntax).

Bergi
  • 630,263
  • 148
  • 957
  • 1,375