Can't set the property in an embedded object

I have defined the following object that contains another object.:

/**
 * @type { {clientNo:Number, id:Number, seq:{sequenceNoOld:Number, sequenceNoNew:Number}, uuid:String, year:Number } }
 */
var documentCircleSeqBackup = { };

If I try to set a property of the embedded object like:

documentCircleSeqBackup.seq.sequenceNoNew = 7;

I get the following error.:

Exception Object: org.mozilla.javascript.EcmaError: TypeError: Cannot set property "sequenceNoNew" of undefined to "7"
MSG: TypeError: Cannot set property "sequenceNoNew" of undefined to "7"
<null>

The error message is telling you that you have an undefined object: seq
So you should first create a new object something like this:

var seqObj = new Object()
seqObj.sequenceNoNew = 7

Now you can add it to the other object

documentCircleSeqBackup.seq = seqObj

Hi Sebastian,

This:

deezzub:
@type { {clientNo:Number, id:Number, seq:{sequenceNoOld:Number, sequenceNoNew:Number}, uuid:String, year:Number } }

Is just documentation on your variable.

As you declare your variable like this:

deezzub:
var documentCircleSeqBackup = { };

it doesn’t contain a property ‘seq’, therefore you get the exception.

Try declare it as below and you’ll be fine:

var documentCircleSeqBackup = {clientNo:null, id:null, seq:{sequenceNoOld:null, sequenceNoNew:null}, uuid:null, year:null };

If you don’t want to declare like this, you’ll need to do something like this:

if('seq' not in documentCircleSeqBackup) {
     documentCircleSeqBackup.seq = {};
}
documentCircleSeqBackup.seq.sequenceNoNew = 7;

Why does this seem wrong to me? I should not have to declare all properties of a JS object at initialization.

var o = {propA: 'string'};
o.propB = 'another string'; // this should be OK in JS but generates a warning in Servoy. Why?

Declaring all properties at initialization may create problems with program flow in some cases (for example when there is a “for (var in obj) loop” it would require an extra “if”).

Hi Sergei,

sergei.sheinin:
Why does this seem wrong to me? I should not have to declare all properties of a JS object at initialization.

var o = {propA: 'string'};

o.propB = ‘another string’; // this should be OK in JS but generates a warning in Servoy. Why?

Actually that code doesn’t generate any warnings at all.

You don’t have to declare the properties of a JS object. It’s just so that that the JS Editor can do the code completion of these objects and give you helpful warnings when you made a typo somewhere.
But you could also ignore any of these warnings, it won’t change the behavior of the code when you execute it.
Also you can suppress certain warnings by right-clicking on the warning marker on the left gutter. This will show a popup menu with options that may include the option to suppress a certain warning.

Hope this helps.

this works:

var obj = {a : 'prop A'}

// to add obj.b:
obj = {a: obj.a, b: 'prop B'}

Hi Sergei,

Both approaches ‘work’.
What is exactly the problem you are having. What are these warnings you are talking about ?

I’m trying to understand why there is a warning on the top one.

Hi Sergei,

I can’t reproduce this.
[attachment=0]Screen Shot 2014-09-09 at 10.40.56.png[/attachment]

Can you show me how you declare the variable? Also what version of Servoy is this?

Screen Shot 2014-09-09 at 10.40.56.png

try assigning the whole object to {}

Hi Sergei,

Using your example I can reproduce it.
[attachment=0]Screen Shot 2014-09-09 at 11.10.58.png[/attachment]
But only when I enter that last line. When you leave that out the warning doesn’t show.
It seems the parser gets confused by the fact you re-initialize the whole object.

Perhaps Johan can comment on this.

Screen Shot 2014-09-09 at 11.10.58.png

i guess this happens because some validations are lazy (done after everything is parsed)
so the end result the o is {}

What happens if you first really type it at declaration?

you can create a case for this is this is really a problem