1

I am working with Socket.io and MongoDB. When I first send my socket.emit to the server, the server takes in the parameter and returns with my desired output. When I reach the socket.on on my client.js the OrderID is shown to the console. Yet when I exit the socket.on method block, the GenOrderID variable becomes undefined.

My question is: why are variables that were created before the socket.on, no longer accessible outside it.

Here is the client.js I am working with:

// Create order array
var Order = [];

// Create GeneratedOrderID variable
var GenOrderID;

// Get School ID from cookie
var SchoolID = getCookie("SchID");

// Generate OrderID
socket.emit('GenerateOrderID', SchoolID);

socket.on('GenerateOrderID', function(GeneratedOrderID) {
    console.log("OrderID sent from server is: " + GeneratedOrderID); // This returns the desired number
    GenOrderID = GeneratedOrderID;
});

Order[0] = GenOrderID;
console.log("GenOrderID is: " + GenOrderID); // This returns undefined
console.log("Order[0] is: " + Order[0]); // This returns undefined

Here is the server.js I am working with:

socket.on('GenerateOrderID', function(PassSchoolID) {
    // Connect to database
    MongoClient.connect('mongodb://localhost:27017/Orders', function(err, db) {
        // Handle errors
        assert.equal(null, err);
        console.log("Begin creation of OrderID");
        db.collection('Orders').find({SchoolID: PassSchoolID}).sort({amount: -1}).limit(1).forEach(function (result) {  
            var GeneratedOrderID = parseInt(result.OrderID);
            GeneratedOrderID++;
            console.log("The server says the OrderID is: " + GeneratedOrderID); // This returns the desired number

            // Return to client
            socket.emit('GenerateOrderID', GeneratedOrderID);
        });
    });
});
  • Your variables are accessible outside, but your `console.log()` is firing before `socket.on()`, which is an asynchronous mechanism. – dvlsg Jul 07 '16 at 19:23
  • @dvlsg So the `console.log()`'s outside the `socket.on()` are firing afterwards? – Josh Myerson Jul 07 '16 at 19:24
  • The general flow of this will (most likely) be: client emit, client setup `.on()` callback, client `console.log()` fires, server receives `GenerateOrderID`, server emits `GenerateOrderID`, client receives `GenerateOrderID`, client sets `GenOrderID` -- but the `console.log()` is long done by that point. You could try reading [this](http://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call), if you haven't yet. It's not quite what you're utilizing here (which would be more like an EventEmitter), but the general concept is the same. – dvlsg Jul 07 '16 at 19:27
  • @dvlsg Thank you for simplifying the flow! I just moved the `console.log()` to inside the `socket.on` – Josh Myerson Jul 07 '16 at 19:30

2 Answers2

1

By placing the console.log() inside the socket.on I was able to have it work properly.

// Create order array var 
Order = [];

// Create GeneratedOrderID variable
var GenOrderID;

// Get School ID from cookie
var SchoolID = getCookie("SchID");

// Generate OrderID
socket.emit('GenerateOrderID', SchoolID);

socket.on('GenerateOrderID', function(GeneratedOrderID) {
    console.log("OrderID sent from server is: " + GeneratedOrderID);
    GenOrderID = GeneratedOrderID;

    Order[0] = GenOrderID;
    console.log("GenOrderID is: " + GenOrderID);
    console.log("Order[0] is: " + Order[0]);
});
0

You're setting the value of GenOrderID inside a callback function, which is only executed once a GenerateOrderId event has occurred. The code inside of a callback function doesn't run until that function is called.

For example:

function run(f) {
    // call `f` after 1 second
    setTimeout(f, 1000);
};

var foo;

run(function() {
    foo = 'bar';
    console.log('in callback:', foo);
});

console.log('before callback:', foo);

// output:
// before callback: undefined
// in callback: 'bar'

At some point in the future, foo will equal 'bar', but you can only know this is the case after your callback has been called. And you can only know that your callback has been called from inside it.

Spencer Judd
  • 409
  • 3
  • 5