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 atcheckDatabase
(=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
3 Answers
Reset to default 2Because you
- do not return anything from you method
- 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
版权声明:本文标题:javascript - Why is my function returning "undefined" instead of boolean - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744288242a2598986.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论