Author Topic: Undefined variable in javascript script  (Read 1552 times)

Thomas Arnbjerg

  • EA User
  • **
  • Posts: 75
  • Karma: +0/-0
    • View Profile
Undefined variable in javascript script
« on: January 18, 2023, 10:15:28 pm »
Hi all,

When I'm debugging a javascript script containing the following fragment the variable 'traceElement' is reported as 'undefined' at the first line containing 'traceElement.'. Any suggestions?
..
if (currentRequirement.Type == "Requirement"){
   Session.Output( "Found link between " + currentUseCase.Alias + " to " + currentRequirement.Alias );
   var traceElement as EA.Element;               
   traceElement = tracePackage.Elements.AddNew( currentUseCase.Alias + " to " + currentRequirement.Alias, "");
   traceElement.TaggedValues.AddNew("from", currentUseCase.Alias);
   traceElement.TaggedValues.AddNew("to", currentRequirement.Alias);
   traceElement.Update();
   tracePackage.Elements.Refresh();
}
..
« Last Edit: January 18, 2023, 10:29:12 pm by Thomas Arnbjerg »

Paolo F Cantoni

  • EA Guru
  • *****
  • Posts: 8551
  • Karma: +254/-129
  • Inconsistently correct systems DON'T EXIST!
    • View Profile
Re: Undefined variable in javascript script
« Reply #1 on: January 18, 2023, 11:01:28 pm »
Hi all,

When I'm debugging a javascript script containing the following fragment the variable 'traceElement' is reported as 'undefined' at the first line containing 'traceElement.'. Any suggestions?
..
if (currentRequirement.Type == "Requirement"){
   Session.Output( "Found link between " + currentUseCase.Alias + " to " + currentRequirement.Alias );
   var traceElement as EA.Element;               
   traceElement = tracePackage.Elements.AddNew( currentUseCase.Alias + " to " + currentRequirement.Alias, "");
   traceElement.TaggedValues.AddNew("from", currentUseCase.Alias);
   traceElement.TaggedValues.AddNew("to", currentRequirement.Alias);
   traceElement.Update();
   tracePackage.Elements.Refresh();
}
..
Hi Thomas,
The first thing I'd do is to change the name to see if it was name related or structural.


Paolo
Inconsistently correct systems DON'T EXIST!
... Therefore, aim for consistency; in the expectation of achieving correctness....
-Semantica-
Helsinki Principle Rules!

Thomas Arnbjerg

  • EA User
  • **
  • Posts: 75
  • Karma: +0/-0
    • View Profile
Re: Undefined variable in javascript script
« Reply #2 on: January 18, 2023, 11:11:30 pm »
Thanks for the tip.

Changed the name to foobar - did not solve the problem. Also I tried to make the variable global (yikes) - also without solving the issue.

Also the variable does not appear under 'locals'

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 12996
  • Karma: +544/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: Undefined variable in javascript script
« Reply #3 on: January 18, 2023, 11:32:20 pm »
Did the addNew operation fail for some reason? (because you passed an empty string as the type)

It might have failed silently. IIRC there is a getlatestError thing somewhere in like Repository. It might help, but I'm not sure.

Geert

Thomas Arnbjerg

  • EA User
  • **
  • Posts: 75
  • Karma: +0/-0
    • View Profile
Re: Undefined variable in javascript script
« Reply #4 on: January 18, 2023, 11:39:30 pm »
Modified the script as follows. 'lastError' is empty in both locations.
'newElem' (renamed variable) is now reported as undefined on the line after the second assignment to 'lastError'. 'newElem' still does not appear under 'locals'.

..
if (currentRequirement.Type == "Requirement"){
    var newElem as EA.Element;
    var lastError;
    lastError = Repository.GetLastError();
    Session.Output( "Found link between " + currentSre.Alias + " to " + currentRequirement.Alias );                                    
    newElem = tracePackage.Elements.AddNew( currentSre.Alias + " to " + currentRequirement.Alias, "");                     
    lastError = Repository.GetLastError();
    newElem.TaggedValues.AddNew("from", currentSre.Alias);
    newElem.TaggedValues.AddNew("to", currentRequirement.Alias);
    newElem.Update();
    tracePackage.Elements.Refresh();
}
..

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 12996
  • Karma: +544/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: Undefined variable in javascript script
« Reply #5 on: January 18, 2023, 11:43:28 pm »
Try passing a type as second parameter to the AddNew operation. An empty string is not a valid type.

Geert

Thomas Arnbjerg

  • EA User
  • **
  • Posts: 75
  • Karma: +0/-0
    • View Profile
Re: Undefined variable in javascript script
« Reply #6 on: January 18, 2023, 11:48:00 pm »
That did it! Thanks! Set the second parameter to 'Class'

Eve

  • EA Administrator
  • EA Guru
  • *****
  • Posts: 8018
  • Karma: +118/-20
    • View Profile
Re: Undefined variable in javascript script
« Reply #7 on: January 23, 2023, 02:12:33 pm »
I'm guessing your tags not working will be your next issue.
Code: [Select]
var newTag as EA.TaggedValue;
newTag = newElem.TaggedValues.AddNew("from", currentSre.Alias);
newTag.Update();
newTag = newElem.TaggedValues.AddNew("to", currentRequirement.Alias);
newTag.Update();