Myna.Query undefined

2 views
Skip to first unread message

Tony Zakula

unread,
Nov 28, 2010, 7:05:15 PM11/28/10
to mynajs-general
I have the code below. and the output that it produces. There are
only fourteen records in the db, and no empty records as I have a
primary key and every record has a code value.

I was wondering what may be causing those undefined values to be in
the qry.data object as it causes problems when mapping codes to other
objects.

Thanks!

Tony Z

function setAppSettings() {
var x;
var obj;
var i = 0;
var qry = new Myna.Query({
dataSource:"ssjs",
sql:"select * from setting"});
if (qry.data.length > 0) {
for (x in qry.data) {
obj = qry.data[x];
i += 1;
Myna.println(i + " " + obj.code);
}
}
}

setAppSettings();

Output:

1 undefined
2 siteTheme
3 undefined
4 appDirectory
5 seoFileExtension
6 seoProductMarker
7 seoPageMarker
8 storeName
9 customerURL
10 secureCustomerURL
11 administratorURL
12 charset
13 cachecontrol
14 expires
15 robots
16 headerLinks

Mark Porter

unread,
Nov 29, 2010, 12:35:41 PM11/29/10
to mynajs-...@googlegroups.com
The problem is using "for (x in array)" syntax. That will loop over
every property in the array, not just the numeric ones. What you want
to use here is Array.forEach like so:

qry.data.forEach(function(obj,i){


i += 1;
Myna.println(i + " " + obj.code);

})

As a bonus, because "obj" and "i" are now local argument variables
there is no need to "var" them at the top of the function.
----------------------------------------------------------
Mark Porter

Myna JavaScript Application Server
Easy web development with server-side JavaScript
http://www.mynajs.org

> --
> You received this message because you are subscribed to the Google Groups "MynaJS-General" group.
> To post to this group, send email to mynajs-...@googlegroups.com.
> To unsubscribe from this group, send email to mynajs-genera...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/mynajs-general?hl=en.
>
>

Tony Zakula

unread,
Nov 29, 2010, 1:00:38 PM11/29/10
to mynajs-...@googlegroups.com
Ah, thanks. Still climbing the JavaScript curve I guess. :-)

Tony Zakula

Reply all
Reply to author
Forward
0 new messages