admin管理员组

文章数量:1401849

        function checkDatabase(){
            var query = document.getElementById("input").value;
            var modQuery = query.split("@")[1];
            var url = ".html/?id="+modQuery;

            $.getJSON(url, function(data) {
                $.each(data, function(i, item) {
                    console.log(item);
                    if(item.length < 1){
                        return false;
                    } else {
                        searchResult = {
                            'name':item[0].screen_name,
                            'loc':item[0].location,
                            'tweet':item[0].tweets[0].tweet_text
                        };
                        return true;
                    }
                });
            });
        }

        function searchForUser(){
            var result = checkDatabase();
            console.log(result);
            if(result){
                console.log(searchResult);          
            } else {
                input.setCustomValidity("Sorry it seems you haven't tweeted about every1speaks yet!");
            }   
        }

I can't understand what it going wrong here, I've seen suggestions at AJAX calls are async (does that mean that they happen when the page is loading?) how can i tweak this to work?

        function checkDatabase(){
            var query = document.getElementById("input").value;
            var modQuery = query.split("@")[1];
            var url = "http://www.somesite./index.html/?id="+modQuery;

            $.getJSON(url, function(data) {
                $.each(data, function(i, item) {
                    console.log(item);
                    if(item.length < 1){
                        return false;
                    } else {
                        searchResult = {
                            'name':item[0].screen_name,
                            'loc':item[0].location,
                            'tweet':item[0].tweets[0].tweet_text
                        };
                        return true;
                    }
                });
            });
        }

        function searchForUser(){
            var result = checkDatabase();
            console.log(result);
            if(result){
                console.log(searchResult);          
            } else {
                input.setCustomValidity("Sorry it seems you haven't tweeted about every1speaks yet!");
            }   
        }

I can't understand what it going wrong here, I've seen suggestions at AJAX calls are async (does that mean that they happen when the page is loading?) how can i tweak this to work?

Share Improve this question asked Jan 6, 2012 at 11:47 Caius EugeneCaius Eugene 8554 gold badges14 silver badges26 bronze badges 2
  • Where's the return statement at checkDatabase (=no return value)? Where's the callback method for the Asynchronous request? (=no expected return value). – Rob W Commented Jan 6, 2012 at 11:50
  • AJAX calls being async means they run outside of the normal flow of code. Normally, your code runs from top to bottom, and if you call a function, the code doesn't continue running until the function has finished. But AJAX calls only stop your code for a brief moment, they don't wait for the AJAX call to load. The code continues, then when the AJAX call has finished, it runs the function you passed to it. – Nathan Commented Jan 6, 2012 at 11:51
Add a ment  | 

3 Answers 3

Reset to default 2

Because you

  1. do not return anything from you method
  2. even if you try to return, since you are doing an asynchronous call (AJAX) which pletes after your function has returned its value, you will be unable to return the ajax call result...

You will need to put the logic in the callback method of the getJSON call.

In neither function do you have a return statement. Both of them will always return undefined.

Update: You have added a return statement to only one of your functions. The other will still always return undefined. That is the return value of any function that exits without execution passing through a return statement.

Try this code, first, you must understand why this code works

check_if_offer_exist_in_shopping_cart(offer_id, custumer_shopping_cart) {

    let x = false
    custumer_shopping_cart.forEach(element => {
      if(offer_id === element){
        this.mediator.openDynamicSnackBar('This offer already exist in shopping cart','info','ok');
        x = true;
        console.log(offer_id);
        return x;
      } 
    });

    return x;

  }


// Check if the user has this offer in his shopping cart already
    const check = this.check_if_offer_exist_in_shopping_cart(offer_id, custumer_shopping_cart);
    console.log('RESULT => ', check);

    if(check){
      this.mediator.openDynamicSnackBar('item already exist in your shopping cart','success','ok');
    } else {

      this.api_customer.add_offer_to_shopping_cart({'id' : offer_id}).subscribe( snap => {
        console.log(snap);
        this.mediator.openDynamicSnackBar('item added to your shopping cart','success','ok');
      }, error => {
        console.log(error);
      });

    }

本文标签: javascriptWhy is my function returning quotundefinedquot instead of booleanStack Overflow