Reputation: 41
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);
});
});
});
Upvotes: 1
Views: 1080
Reputation: 409
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.
Upvotes: 0
Reputation: 41
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]);
});
Upvotes: 1