简体   繁体   English

Node.js模块级变量未定义

[英]Node.js module level variables are undefined

I'm having an issue accessing the scope of a module level variable from within a function inside of said module. 我在从所述模块内部的函数内访问模块级变量的范围时遇到问题。 See below... 见下文...

var socketio = require('socket.io');
var socket = socketio.listen();

var myCustomModule = require('./lib/mycustommodule')('http://mysite:8080');

socket.on('connection', function connection(socket) {

    socket.emit('message', {data:"test1"});  <====THIS WORKS

    socket.on('init', function init(data) {

        socket.emit('message', {data:"test1"});  <====THIS WORKS

        refreshMyCustomModule();
    });

});

var refreshMyCustomModule = function() {

    socket.emit('message', {data:"test1"}); <=====THIS DOESN'T WORK

    myCustomModule.beginSomeAsyncTask(function(data) { <======THIS DOESN'T WORK

         socket.emit('message', {data:"test2"}); <========THIS DOESN'T WORK

    });

};

Looking at the sample above. 看看上面的示例。 When I call my refreshMyCustomModule function suddenly socket and myCustomModule become undefined. 当我调用我的refreshMyCustomModule函数时, socketmyCustomModule变得未定义。 I've also tried using this as well as setting up a var self = this . 我也试过使用它以及设置var self = this

I've written a bunch in javascript on the client but when coding in node.js it seems like scoping is different and I just can't crack this nut. 我在客户端上用javascript编写了一堆,但是当在node.js中进行编码时,似乎范围不同,我只是无法破解这个问题。

Note that the socket at the global level of your script and socket within your function connection are two different variables . 请注意, socket在你的脚本和全球层面socket的内部function connection 是两个不同的变量 The one inside your function connection is the argument that was passed into that function from the connection event. function connection中的一个是从连接事件传递给该函数的参数 The one you're using in refreshMyCustomModule is the global one, the one on which you called listen . 您在refreshMyCustomModule使用的那个是全局的,您调用listen那个。

This is clearer if we change their names, since they're different variables: 如果我们更改名称,这一点会更清楚,因为它们是不同的变量:

var socketio = require('socket.io');
var socketUsedForListen = socketio.listen();

var myCustomModule = require('./lib/mycustommodule')('http://mysite:8080');

socketUsedForListen.on('connection', function connection(socketFromConnection) {

    socketFromConnection.emit('message', {data:"test1"});

    socketFromConnection.on('init', function init(data) {

        socketFromConnection.emit('message', {data:"test1"});

        refreshMyCustomModule();
    });

});

var refreshMyCustomModule = function() {

    socketUsedForListen.emit('message', {data:"test1"});

    myCustomModule.beginSomeAsyncTask(function(data) {

         socketUsedForListen.emit('message', {data:"test2"});

    });

};

I'm reasonably certain you meant to use socketFromConnection in refreshMyCustomModule , not socketUsedForListen . 我有理由相信你打算refreshMyCustomModule使用socketFromConnection ,而不是socketUsedForListen If so, either move the refreshMyCustomModule into your connection callback, or pass the socket into it as an argument. 如果是这样,请将refreshMyCustomModule移动连接回调中,或将套接字作为参数传递给它。

It's because you're using an async event in socket.on('init') that leads to the scope issue. 这是因为你在socket.on('init')中使用异步事件会导致范围问题。 I believe if you pass in the parameters you want to use from the parent it will work. 我相信如果你传递你想要使用的参数,它将起作用。 eg: 例如:

var socketio = require('socket.io');
var socket = socketio.listen();

var myCustomModule = require('./lib/mycustommodule')('http://mysite:8080');

socket.on('connection', function connection(socket) {
    socket.emit('message', {data:"test1"});  <====THIS WORKS

    socket.on('init', function init(data) {

        socket.emit('message', {data:"test1"});  <====THIS WORKS

        refreshMyCustomModule(socket, myCustomModule);
    });

});

var refreshMyCustomModule = function(socket, module) {

    socket.emit('message', {data:"test1"}); <=====THIS DOESN'T WORK

    module.beginSomeAsyncTask(function(data) { <======THIS DOESN'T WORK

         socket.emit('message', {data:"test2"}); <========THIS DOESN'T WORK

    });

};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM