Incorrect Code Example in Wiki

Here I am referring to the link:- https://github.com/dreamfactorysoftware/dsp-core/wiki/API-Access-via-Scripts

//  Retrieve all contacts from the Contacts database
var _records = platform.api.get( "db/Contacts" );

//  Uppercase each name...
_.each( _records, function( record ) {
    record.name = record.name.toUpperCase();
});

The Above code does not work as expected, as the record.name seems to be undefined. The way to access the records seems to be working this way:-
_.each( _records, function( record ) {
print (“record is” + JSON.stringify(record));
print (“first is” + record[0].name);
}
So, the record is actually an array, which needs an index. _each as expected is not pointing to each record of the array, and record is still pointing to the array.

Please let me know which is correct. Is it a bug, or Wiki is defined incorectly.

As I see this would work ok:-
// Retrieve all contacts from the Contacts database
var _records = platform.api.get( “db/Contacts” );

// Uppercase each name…
_.each( _records.record, function( record ) {
record.name = record.name.toUpperCase();
});

I have tried accessing the record inside the _records now. It seems that is the structure.

Correct. _records looks like this:

{
	"record": [
		{
			"id": 1,
			"name": "testrecord"
		}
	]
}

_records.record is an array.

Absolutely, that’s why I was requesting that you update the code in Wiki, as people refer to that.
Instead of _records, it should be _records.record in the each loop.

Wiki has been updated. Thanks for catching that.

1 Like