Can't set the property in an embedded object

Discuss all problems you have with Servoy here. It might help to mention the Servoy version and Operating System version you are using

Can't set the property in an embedded object

Postby deezzub » Fri Mar 28, 2014 11:19 am

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

Code: Select all
/**
* @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:

Code: Select all
documentCircleSeqBackup.seq.sequenceNoNew = 7;


I get the following error.:

Code: Select all
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>
deezzub
 
Posts: 328
Joined: Tue May 28, 2013 3:02 pm
Location: Oldenburg, Germany

Re: Can't set the property in an embedded object

Postby Thomas Parry » Fri Mar 28, 2014 2:36 pm

The error message is telling you that you have an undefined object: seq
So you should first create a new object something like this:
Code: Select all
var seqObj = new Object()
seqObj.sequenceNoNew = 7


Now you can add it to the other object
Code: Select all
documentCircleSeqBackup.seq = seqObj
Tom Parry
Prospect IT
Java/C++/Servoy/Jasper Reports/Simulation/Service Applications
http://www.prospect-saas.biz
Thomas Parry
 
Posts: 498
Joined: Thu Jan 10, 2008 8:48 pm
Location: Ottawa, Canada

Re: Can't set the property in an embedded object

Postby mboegem » Fri Mar 28, 2014 2:39 pm

Hi Sebastian,

This:
deezzub wrote:@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 wrote:var documentCircleSeqBackup = { };


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

Try declare it as below and you'll be fine:
Code: Select all
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:
Code: Select all
if('seq' not in documentCircleSeqBackup) {
     documentCircleSeqBackup.seq = {};
}
documentCircleSeqBackup.seq.sequenceNoNew = 7;
Marc Boegem
Solutiative / JBS Group, Partner
Servoy Specialist
• Servoy Certified Developer
• Servoy Valued Professional
• Freelance Developer

Image
User avatar
mboegem
 
Posts: 1748
Joined: Sun Oct 14, 2007 1:34 pm
Location: Amsterdam

Re: Can't set the property in an embedded object

Postby sergei.sheinin » Thu Sep 04, 2014 10:48 am

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

Code: Select all
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").
Sergei Sheinin
JavaScript, RDBMS
http://js2dx.com
sergei.sheinin
 
Posts: 79
Joined: Wed May 07, 2014 3:22 pm

Re: Can't set the property in an embedded object

Postby ROCLASI » Thu Sep 04, 2014 5:02 pm

Hi Sergei,

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

Code: Select all
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.
Robert Ivens
SAN Developer / Servoy Valued Professional / Servoy Certified Developer

ROCLASI Software Solutions / JBS Group, Partner
Mastodon: @roclasi
--
ServoyForge - Building Open Source Software.
PostgreSQL - The world's most advanced open source database.
User avatar
ROCLASI
Servoy Expert
 
Posts: 5438
Joined: Thu Oct 02, 2003 9:49 am
Location: Netherlands/Belgium

Re: Can't set the property in an embedded object

Postby sergei.sheinin » Tue Sep 09, 2014 9:51 am

this works:

Code: Select all
var obj = {a : 'prop A'}

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

Sergei Sheinin
JavaScript, RDBMS
http://js2dx.com
sergei.sheinin
 
Posts: 79
Joined: Wed May 07, 2014 3:22 pm

Re: Can't set the property in an embedded object

Postby ROCLASI » Tue Sep 09, 2014 9:55 am

Hi Sergei,

Both approaches 'work'.
What is exactly the problem you are having. What are these warnings you are talking about ?
Robert Ivens
SAN Developer / Servoy Valued Professional / Servoy Certified Developer

ROCLASI Software Solutions / JBS Group, Partner
Mastodon: @roclasi
--
ServoyForge - Building Open Source Software.
PostgreSQL - The world's most advanced open source database.
User avatar
ROCLASI
Servoy Expert
 
Posts: 5438
Joined: Thu Oct 02, 2003 9:49 am
Location: Netherlands/Belgium

Re: Can't set the property in an embedded object

Postby sergei.sheinin » Tue Sep 09, 2014 10:24 am

I'm trying to understand why there is a warning on the top one.
Attachments
propormeth.png
propormeth.png (7.61 KiB) Viewed 10631 times
Sergei Sheinin
JavaScript, RDBMS
http://js2dx.com
sergei.sheinin
 
Posts: 79
Joined: Wed May 07, 2014 3:22 pm

Re: Can't set the property in an embedded object

Postby ROCLASI » Tue Sep 09, 2014 10:42 am

Hi Sergei,

I can't reproduce this.
Screen Shot 2014-09-09 at 10.40.56.png
No warnings
Screen Shot 2014-09-09 at 10.40.56.png (12.8 KiB) Viewed 10630 times


Can you show me how you declare the variable? Also what version of Servoy is this?
Robert Ivens
SAN Developer / Servoy Valued Professional / Servoy Certified Developer

ROCLASI Software Solutions / JBS Group, Partner
Mastodon: @roclasi
--
ServoyForge - Building Open Source Software.
PostgreSQL - The world's most advanced open source database.
User avatar
ROCLASI
Servoy Expert
 
Posts: 5438
Joined: Thu Oct 02, 2003 9:49 am
Location: Netherlands/Belgium

Re: Can't set the property in an embedded object

Postby sergei.sheinin » Tue Sep 09, 2014 11:04 am

try assigning the whole object to {}
Attachments
propormeth.png
propormeth.png (2.71 KiB) Viewed 10628 times
Sergei Sheinin
JavaScript, RDBMS
http://js2dx.com
sergei.sheinin
 
Posts: 79
Joined: Wed May 07, 2014 3:22 pm

Re: Can't set the property in an embedded object

Postby ROCLASI » Tue Sep 09, 2014 11:15 am

Hi Sergei,

Using your example I can reproduce it.
Screen Shot 2014-09-09 at 11.10.58.png
Warning
Screen Shot 2014-09-09 at 11.10.58.png (14.19 KiB) Viewed 10626 times

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.
Robert Ivens
SAN Developer / Servoy Valued Professional / Servoy Certified Developer

ROCLASI Software Solutions / JBS Group, Partner
Mastodon: @roclasi
--
ServoyForge - Building Open Source Software.
PostgreSQL - The world's most advanced open source database.
User avatar
ROCLASI
Servoy Expert
 
Posts: 5438
Joined: Thu Oct 02, 2003 9:49 am
Location: Netherlands/Belgium

Re: Can't set the property in an embedded object

Postby jcompagner » Wed Sep 10, 2014 12:39 pm

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
Johan Compagner
Servoy
User avatar
jcompagner
 
Posts: 8833
Joined: Tue May 27, 2003 7:26 pm
Location: The Internet


Return to Discuss possible Issues and Bugs

Who is online

Users browsing this forum: No registered users and 3 guests