0

Hi i am new to jquery/javascript. I have created a global variable. I set the value of value of this variable inside function like this:

var arr;
CreatecheckPassword: function() {

                var pas = document.getElementById("pos-password")

                var user = new db.web.Model("res.users").get_func("read")(this.session.uid, ['password']).pipe(function(result) {
                     arr = result.password;
                     alert(arr);//prints the expected value
                });
                alert(arr);//prints nothing
},

I don't know where i am doing wrong. I tried hard but was unable to find the solution. Can someone help.

6
  • You have a couple syntax errors there... Commented Mar 10, 2012 at 9:13
  • 2
    I would guess that your database access function is asynchronous, which means control continues to the alert that prints nothing before the callback is executed and the arr variable is set. Commented Mar 10, 2012 at 9:16
  • How can then i set the value of this variable. Can't do without database operation. Commented Mar 10, 2012 at 9:21
  • Why can't you move the second alert into a separate function and call it in database callback function? Commented Mar 10, 2012 at 9:35
  • 2
    Basically you have to restructure your code so that anything that uses the return from an asynchronous function is included within the callback that you pass to the function (or included in a separate function called from that callback). You can probably find some good explanations of Async JavaScript with Google - if you can't find any specifically about database access look at some Ajax tuturials because exactly the same async callback issue occurs with Ajax calls. Commented Mar 10, 2012 at 9:46

1 Answer 1

1

jslint or jshint is your friend. Get it and use it on your code to get lint messages.

enter image description here

Sign up to request clarification or add additional context in comments.

2 Comments

But none of the errors/warnings shown have anything to do with the problem, indeed the problem is not caused by a syntax error...
The problem isn't stated in the question, then. In fact the original question isn't clear at all on the nature of the problem. Also the title of the post is wrong - the question is not at all about how to "Set a global variable". Reading the comments, it's something about asynch invocation of ajax, I think.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.