Caius Eugene
Caius Eugene

Reputation: 855

Why is my function returning "undefined" instead of boolean

        function checkDatabase(){
            var query = document.getElementById("input").value;
            var modQuery = query.split("@")[1];
            var url = "http://www.somesite.com/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?

Upvotes: 0

Views: 12114

Answers (3)

user7956520
user7956520

Reputation:

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

    }

Upvotes: 0

Gabriele Petrioli
Gabriele Petrioli

Reputation: 195972

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 completes 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.

Upvotes: 2

Marcin
Marcin

Reputation: 49816

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.

Upvotes: 2

Related Questions