How to override javascript variable? - Stack Overflow

I am working on an Express.io mini project and I am stuck on this variable override problem.Here is my

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 badges
Add a ment  | 

3 Answers 3

Reset to default 3

I'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)
});

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745454001a4628401.html

相关推荐

  • How to override javascript variable? - Stack Overflow

    I am working on an Express.io mini project and I am stuck on this variable override problem.Here is my

    5小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信