0

As a learning exercise, I'm trying to query ScriptDb and return a person's name when my script has their email address. The function below returns "ScriptDbResult" instead of the correct name (which should be Patrick Farmer - see the list below the function). Can somebody please advise on what I'm doing wrong?

function getNameFromDb() {
var email = "[email protected]";
var db = ScriptDb.getMyDb();
var result = db.query(
    {Names: {Emails: email}});
Logger.log(result);
}

This is what the Logger shows is in the database:

{
  "Emails": "[email protected]",
  "Names": "Luca Jenkins"
}
{
  "Emails": "[email protected]",
  "Names": "Patrick Farmer"
}
{
  "Emails": "[email protected]",
  "Names": "Elizabeth Jennings"
}
{
  "Emails": "[email protected]",
  "Names": "Gabriel Ryan"
}

1 Answer 1

2

You are almost there. You indeed get back a ScriptDBResult object. You need to fecth results using the next method

while (result.hasNext()){
  var obj = result.next(); 
  Logger.log(obj.Names);
  Logger.log(obj.Emails);
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for such a rapid response Srik. I got an error message to start with but when I changed my query from 'var result = db.query( {Names: {Emails: email}});' to 'var result = db.query( {Emails: email});' it worked a treat.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.