UPDATED CODE: i, I am new to Javascript programming and get an undefined variable when trying to assign a new variable from a method.
I use node.js and create a redis server using redis-client in a "client variable".
var redis = require("redis");
var client = redis.createClient();
client.on("error", function (err) {
console.log("Error " + err); });
var numberPosts;
client.get("global:nextPostId", function(err, replies) {
numberPosts = replies;
console.log(numberPosts);
});
console.log(numberPosts);
When I call console.log inside the callback function, it returns the correct value, however, when I call console.log outside the callback function, it returns "undefined". I am trying to assign a value that is inside a callback function to the global variable numberPosts.
Any help is greatly appreciated, thanks.
Matt
source
share