Jamie5245261
Posts: 0
Joined: Thu Feb 20, 2014 2:01 pm

Why does the following server code only return the first database entry it finds?

I'm new to appery and am walking through the tutorials and experimenting purely for learning purposes.

I have a database with a long list of words in it. In fact, it's a Scrabble-like list of all of the words in English. I wish to return all of the words in the database that begin with a specific string of letters. Again, for learning purposes, I want to do this server-side (not application side).

So far, so good. I've found a lot of documentation on this and have successfully achieved what I've set out to do ... with one difficulty: my server code is only returning the first entry it finds in test output. For example, in the code below, I've hard-coded a request to return all words beginning with "da". The script successfully returns "dab", but not "dabble" or "daft", etc. I'm looking to receive all 25 words (or how many ever there are).

The actual response is as follows:

{
"message":"53043eefe4b0e3176f3fa375 dab"
}

So, my question today is: what am I doing wrong? Please see my code, below:

---------------

var responseBody = {},
requestParams = {},
paramKeys = request.keys();

// Code learned from tutorials - this should look familiar to appery staff
for (var key = 0; key < paramKeys.length; key++) {
requestParams[paramKeys[key]] = request.get(paramKeys[key]);
}

// Declare database ID and Master key
var dbId = "123abc...";
var masterKey = "123abc...";

// Get the requested words from request parameters
var word = requestParams['word'];
var db = "https://api.appery.io/rest/1/db/colle..."

try {
// Get the word from the database - here I've hard-cded a request for "da"
var XHRResponse = XHR.send("GET", "https://api.appery.io/rest/1/db/colle...", {
"headers": {
"X-Appery-Database-Id": dbId,
"X-Appery-Master-Key": masterKey
},
"parameters": {
"where": '{"word":{"$regex":"da", "$options":"i"}}'
}
});

// give me the words as well as their database IDs
var retrievedInfo = XHRResponse.body[0]["_id"] + " " + XHRResponse.body[0]["word"];

responseBody.message = retrievedInfo

response.success(responseBody, "application/json");
} catch (e) {
response.success("message: " + e.message + "\ncode: " + e.code); //If something goes wrong, error message appears
}

// what I receive is "message":"53043eefe4b0e3176f3fa375 dab" Can you help?
// PS: yes, I will feel silly once you give me the answer.

Maryna Brodina
Posts: 0
Joined: Thu Apr 05, 2012 7:27 am

Why does the following server code only return the first database entry it finds?

Hello!
You do prevar retrievedInfo = XHRResponse&#46;body[0]["_id"] + " " + XHRResponse&#46;body[0]["word"];/preso you take first word only from entire result and show it. Go through XHRResponse.body in a loop and add all results in retrievedInfo.

Jamie5245261
Posts: 0
Joined: Thu Feb 20, 2014 2:01 pm

Why does the following server code only return the first database entry it finds?

Hi Maryna: thank you for your response. That did the trick. You can consider this case closed.

Just in case someone else in the future is looking to learn something like this, I've included my final code that works. For example, sending "zen" as text returns the following response:

{
"message":"zenaida zenana zenanas zenith zeniths zenaidas zenithal "
}

Believe it or not, that's English.

Disclaimer: I'm not a professional programmer so there are no doubt violations of convention and no doubt someone could produce a more "elegant" version than I have but I feel it important to contribute back to the community.

----------

The script is available for viewing at http://pastebin.com/5y7ZaVGH

Return to “Issues”