Domanda

I'm still having some trouble understanding the scope of variables inside a node.js app. In regular javascript a nested function has access to the variables of it's parents. In node, this doesn't seem the case.

Here i have the file app.js that has a variable 'bookies' and a require 'routes'. The trouble i'm having is that the variable 'bookie' is not available in my routes.js file.

Why is that?

enter image description here

È stato utile?

Soluzione

If you want to have bookie defined in the routes require you should pass the bookie like this:

var routes = require("./routes")(bookie);

This is because in Node, The top-level scope is not the global scope; var something inside a Node module will be local to that module.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top