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?
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.
Because you
You will need to put the logic in the callback
method of the getJSON
call.
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);
});
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.