How to get from a select the result?

49 views
Skip to first unread message

Gabriel Espósito

unread,
May 29, 2016, 11:22:33 PM5/29/16
to lovefield-users
Hello.. I need to do something like:

item = db.getSchema().table("Employees");

employee = db.select(item.surname).
                   from(item).
                   where(item.name.eq(req['data']['Name'])). // req has the name
                   exec().then(function(rows){
                       return rows[0]['surname'];
                   });
                   console.log(employee); // I need the Surname of a given name outside the scope

But I get a Promise and I don't know how to extract the data... 
inside, very deep in the promise, there IS the correct data

Would you help me?

Arthur Hsu

unread,
May 29, 2016, 11:38:33 PM5/29/16
to lovefield-users
Whenever you see a then() function, that means the object on its left-hand-side is likely a promise.
So it's perfectly reasonable employee is a promise (since query execution returns promise).

Lovefield's transaction execution returns promise and run asynchronously. A query execution started an implicit transaction.

Gabriel Espósito

unread,
May 29, 2016, 11:52:21 PM5/29/16
to lovefield-users
Thanks Arthur. The fact is that I don't know how to extract data out of the scope of the "then()" from the promise... would you tell me how?

Arthur Hsu

unread,
May 30, 2016, 12:08:32 AM5/30/16
to lovefield-users
That you need to accomodate Promise into your design. See https://developers.google.com/web/fundamentals/primers/promises/?hl=en to learn about how to use it.

Gabriel Espósito

unread,
May 30, 2016, 1:51:18 AM5/30/16
to lovefield-users
Thanks Arthur for the information... I can not get the variable out the scope of the then... About the promise already can retrieve the info but can not out from the scope of the then()....

dpa...@chromium.org

unread,
May 31, 2016, 2:33:12 PM5/31/16
to lovefield-users
I think that you are not fully understanding how Promises work. Your desire to "get the variable out of the scope" is invalid. Code inside a Promise callback is asynchronous (executes sometime later). You are asking how to get that information synchronously which is not possible. Try the following in your dev console

Promise.resolve().then(function() {console.log('executes later');});
console
.log('executes first');

By the time the 2nd line executes, the Promise callback has not been executed yet. As previous comment suggests your design needs to accommodate asynchronous code.

drdr...@gmail.com

unread,
Apr 10, 2019, 4:01:22 PM4/10/19
to lovefield-users
So annoying that people don't answer your question.
They know exactly what you mean but like to make it difficult.
I am also trying Lovefield but it's hard to learn.

Op maandag 30 mei 2016 05:22:33 UTC+2 schreef Gabriel Espósito:
Reply all
Reply to author
Forward
0 new messages