Skip to content

Instantly share code, notes, and snippets.

@joelklabo
Created January 24, 2011 05:03
Show Gist options
  • Save joelklabo/792860 to your computer and use it in GitHub Desktop.
Save joelklabo/792860 to your computer and use it in GitHub Desktop.
I am not understanding some fundamental js ideas here, what am i missing?
function findBeer(words) {
var search_terms = [];
var rows = [];
//
// I want to assign a variable from the 'getResults' callback
// to this:
//
results = getResults(word); // This is always undefined
console.log(getResults());
}
function getResults(word) {
var db = 'http://localhost:5984/beer_list/_design/beer_key/_view/_full_name?include_docs=true';
var results = [];
var response = [];
var j = 0;
var reg_exp = new RegExp("" + word + "", 'i');
request({uri:db}, function (err, resp, b) {
if (err) throw err;
if (resp.statusCode !== 200) throw new Error("Could not get document. " + b );
response = JSON.parse(b);
return "this was a test"; // This should get assined to results above
for (var i = 0; i < response.rows.length; i += 1) {
if (response.rows[i].key.search(reg_exp) !== -1) {
results[j] = response.rows[i];
j += 1;
}
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment