I am working on an Express.io mini project and I am stuck on this variable override problem.
Here is my code:
get_time_offset = function(timezone_id){
Timezone.findById(timezone_id, function(err, doc){
if(err) {
console.log(err);
} else {
console.log(doc.offset);
}
});
}
This code block can display the doc.offset's value through the console.log without any problem and I would like to make the doc.offset be available outside the Timezone Object. Here's what I have e up so far:
get_time_offset = function(timezone_id){
var offset;
Timezone.findById(timezone_id, function(err, doc){
if(err) {
console.log(err);
} else {
offset = doc.offset;
}
});
console.log(offset);
}
It says 'offset is undefined' and I can't seem to find any other way of solving this.
I am working on an Express.io mini project and I am stuck on this variable override problem.
Here is my code:
get_time_offset = function(timezone_id){
Timezone.findById(timezone_id, function(err, doc){
if(err) {
console.log(err);
} else {
console.log(doc.offset);
}
});
}
This code block can display the doc.offset's value through the console.log without any problem and I would like to make the doc.offset be available outside the Timezone Object. Here's what I have e up so far:
get_time_offset = function(timezone_id){
var offset;
Timezone.findById(timezone_id, function(err, doc){
if(err) {
console.log(err);
} else {
offset = doc.offset;
}
});
console.log(offset);
}
It says 'offset is undefined' and I can't seem to find any other way of solving this.
Share Improve this question edited Feb 5, 2014 at 6:01 Suhaib Janjua 3,57216 gold badges69 silver badges87 bronze badges asked Feb 5, 2014 at 5:55 jolirjolir 311 silver badge5 bronze badges3 Answers
Reset to default 3I'm thinking either change your initial declaration to...
var offset = {};
...or, your console.log is executing before your function has finished. Simple check by adding a log inside the function...
} else {
offset = doc.offset;
console.log('from inside ... ', offset);
}
});
}
console.log('from outside ... ', offset);
... and see if 'outside' fires first.
EDIT:
If 'from outside' is running first, have your follow-up code called by the initial function.
get_time_offset = function(timezone_id){
var offset;
function processResult(val) {
console.log(val);
}
Timezone.findById(timezone_id, function(err, doc){
if(err) {
console.log(err);
} else {
offset = doc.offset;
}
processResult(offset);
});
}
if you want to make offset available inside the function you can pass it as a parameter here is demo http://thomasdavis.github.io/tutorial/anonymous-functions.html
var scope = {offset : 10};
get_time_offset = function(timezone_id){
Timezone.findById(timezone_id, function(err, doc, scope){
if(err) {
console.log(err);
} else {
scope.offset = doc.offset;
}
});
}
console.log(scope.offset);
sorry edited: i made a mistake in the example offset must be an object for it to be passed by reference and modified, heres a fiddle i hacked together of it http://jsfiddle/VLbVw/4/
Try this:
get_time_offset = function(timezone_id, callback){
Timezone.findById(timezone_id, function(err, doc){
if(err) {
console.log(err);
callback(false);
} else {
callback(doc.offset);
}
});
};
get_time_offset(5, function(offset){
console.log(offset)
});