Reputation: 51
After receiving the object from scriptDB I cannot get access to some of its fields. This field with numeric keys. If you uncomment the line
// var item2 = obj2['test']['2']['f']
it will cause a runtime error.
Full code is:
function test() {
var obj1 =
{
type: 'object',
name: 'test',
test:
{
F: 'foo',
B: {f: 'foooo', b: 'baaar', },
1: 'foo',
2: {f: 'foooo', b: 'baaar', },
},
};
var db = ScriptDb.getMyDb();
var id = db.save(obj1).getId();
var obj2 = db.load(id);
db.removeById(id);
var item1 = obj1['test']['2']['f']; // item1 = "foooo"
// var item2 = obj2['test']['2']['f']; // Runtime error!
var item3 = obj2['test']; // item3 = ({'2':{f:"foooo", b:"baaar"}, F:"foo", '1':"foo", B:{f:"foooo", b:"baaar"}})
var item4 = obj2['test']['2']; // item4 = undefined!!! Why?!!!
var item5 = obj1['test']['2']; // item5 = ({f:"foooo", b:"baaar"})
var item6 = obj2['test']['B']; // item6 = ({f:"foooo", b:"baaar"})
var item7 = obj2['test']['B']['f']; // item7 = "foooo"
return
}
As you can see, it is impossible to get object:
var item4 = obj2['test']['2']; // item4 = undefined
At the same time:
var item5 = obj1['test']['2'];
returns correct object from original obj1, and
var item6 = obj2['test']['B'];
returns a similar object from obj2. What's the problem?
P.S. The only difference between obj1 and obj2 (before and after saving them scriptDB) - presence of quotes in the numeric keys in obj2.
Upvotes: 1
Views: 175
Reputation: 17772
You should star issue 3390 (posted by Zig Mandel).
Now talking solutions to your problem. The best thing is to void using numeric keys, but if you can't do that, then an easy workaround is to stringify and re-parse the object returned by the ScriptDb
.
var obj2 = JSON.parse(db.load(id).toJson());
The down-side of doing this that you can't not modify obj2
and then save your updates back to ScriptDb
. If you don't use the object id anywhere else you can always remove the original object and save the "changes" as a new one.
Upvotes: 1
Reputation: 19834
Its a bad idea to use numeric properties thou it should work. It doesnt work because of this: https://code.google.com/p/google-apps-script-issues/issues/detail?id=3390
Upvotes: 0