Michael J Buckley (2) [Avatar] Offline
#1
While I do have a basic understanding of interacting with dbs through a model, I have never been exposed to TaffyDB until the book exercises. The question below may or may not be apropos to Taffy: I don't know what I don't know. smilie

I am confused to why I'm getting a particular response as I follow along with the js console exercise from the 5.14 listing on pgs. 162-163:

// get the person with the id of 'id_03':
var person = peopleDb({ cid : 'id_03' }).first();
>> returns undefined

// the anonymous person should have an id of 'a0'
person = peopleDb({ id : 'a0' }).first();
>> returns Object {cid: "a0", name: "anonymous", css_map: undefined, id: "a0", ___id: "T000002R000002"…}

Why do I get undefined when I declare the variable "person", but an Object is returned when I make (what looks to me to be) a similar assignment?

Thank you. Love the book so far, btw.
michael.mikowski (247) [Avatar] Offline
#2
Re: Listing 5.14 - browser js console returning undefined
On the face of it, this appears to be the case of missing data. In the first case, apparently there is no entry with the id 'id_03'. TaffyDB apparently returns undefined in this case (arguably, it should be returning 'null' instead, since 'null' is the empty object). In the second case the entry exists, and therefore an object is returned.

However, I get a feeling your question is deeper than that.

I am guessing that when using the debugger var person = peopleDB({ cid : 'id_03' }).first(); shows an undefined return context while properly populating a person object; whereas person = peopleDB({ cid : 'id_03' }).first(); shows an Object return context and a properly populated person object. If this is the case, I think what you are seeing is the difference in return contexts from declaration (var ...) and assignment.

The thing to remember when you write:

var foo = '123';

It is really shorthand for:

var foo;

foo = 123;

Both of those have return contexts. The first always return 'undefined'. The second returns the value to the right of the = sign. My guess is var foo = data will always return undefined.

This is why declare all JS variables at the top of a function, and usually separate declaration from assignment.

I hope that was on the right path and answers your question.

Cheers, Mike
Michael J Buckley (2) [Avatar] Offline
#3
Re: Listing 5.14 - browser js console returning undefined
Thank you for your prompt reply.

> However, I get a feeling your question is deeper than that.

You're right in that I do want to make sure I haven't done something wrong at this point before I continue working through the book, but I also want to understand the Why before plowing on. Your answer appears to explain what is going on under the hood.

I think I better understand why you've made such a point to hammer on JavaScript execution context (eg. Chapter 2; this lecture ).
cssgalactic (2) [Avatar] Offline
#4
Re: Listing 5.14 - browser js console returning undefined
Hello Michael

On 5.14 Playing with fake people and liking it, the result is undefied, getting the same result using the sample code.

var peopleDb = spa.model.people.get_db();
undefined

var peopleList = peopleDb().get();
undefined

peopleList;
[Object, Object, Object, Object, Object]

peopleDb().each(function(person, idx){console.log(person.name);});
Object {extend: undefined, filter: function, order: function, limit: function, start: function…}

Any ideas why I'm getting this result?
cssgalactic (2) [Avatar] Offline
#5
Re: Listing 5.14 - browser js console returning undefined
Hello again. Still not able to do testing with the console now on 6.3.3 Test Avatar Support " var $t = $('< div / >') undefined " Maybe it is a simple explanation, a lot of advanced concepts in this chapter, probably time to take a short break smilie

Message was edited by:
cssgalactic

Message was edited by:
cssgalactic

Message was edited by:
cssgalactic

Message was edited by:
cssgalactic
michael.mikowski (247) [Avatar] Offline
#6
Re: Listing 5.14 - browser js console returning undefined
Hi CssGalatic:


Sorry for the delay. I don't get paid for this. My day job, on the other hand, needed tending to smilie


So I'm not sure what problem you are having. Can you please clarify? As an earlier post shows,

var $t = $();

should return undefined. This is not an error, just the nature of combining *declaration* and *assignment*. If you want to see the difference, do this:


var $t; // declaration; returns 'undefined'.


$t = $(); // assignment; if jquery is loaded, it should return an object.


Now maybe there is something else happening. If so, please clarify, and I will try to help smilie
343960 (2) [Avatar] Offline
#7
I am also following along and got stuck in the console testing parts 5.14 and 5.17. I haven't used google chrome console before, but the book implies that if you type in what's printed in bold, you should get back what is in italics. But it doesn't work that way. In 5.17 for example, I have typed in each line as it shows in the book and hit return after every semi-colon. When I type

peopleDb().each(function(person, idx) {console.log(person.name);});

I get back

Object {extend: undefined, filter: function, order: function, limit: function, start: function…}

instead of a list of the people names like it shows in the book.What is wrong?