Mendix App factoryThrew Error

I am getting factoryThrew Error from a Mendix app when I use more than one Mendix Pluggable widget that is developed using GoJS. Below are the error details
factoryThrew Error: factoryThrew
at d (http://localhost:8080/mxclientsystem/mxui/mxui.js?637964586797514234:5:553)
at http://localhost:8080/mxclientsystem/mxui/mxui.js?637964586797514234:5:9535
at Me (http://localhost:8080/mxclientsystem/mxui/mxui.js?637964586797514234:5:9644)
at Me (http://localhost:8080/mxclientsystem/mxui/mxui.js?637964586797514234:5:9280)
at http://localhost:8080/mxclientsystem/mxui/mxui.js?637964586797514234:5:9835
at Fe (http://localhost:8080/mxclientsystem/mxui/mxui.js?637964586797514234:5:9702)
at Be (http://localhost:8080/mxclientsystem/mxui/mxui.js?637964586797514234:5:9774)
at i (http://localhost:8080/mxclientsystem/mxui/mxui.js?637964586797514234:5:10909)
at HTMLScriptElement. (http://localhost:8080/mxclientsystem/mxui/mxui.js?637964586797514234:5:13143)

I observed that above error is thrown by 2nd widget and 1st widget on the page renders properly. For example lets say I developed SampleWidgetOne and SampleWidgetTwo and I don’t see this error if I use anyone of the widget multiple times. But if I use both widgets on same page then this error comes.

I debugged the issue and observed that this error is coming from wherever go.Diagram is used in 2nd widget. Not sure why this problem is coming from 2nd widget and this works fine from widget one that is on the same page. Any help is appreciated.

Note: This error thrown during runtime and not on compile time

So for one type of widget you say you can have multiple instances on a page, but when you try to use a second type of widget it causes that error which is not very helpful because it is missing the error message. I don’t suppose you could find out what the error message actually is? Maybe set a breakpoint somewhere in mxui.js?

My guess is that the second widget is loading the GoJS library a second time. Doing so can cause subtle problems. See if the second widget is trying to load the library even though it doesn’t need to.

I also guessed second widget is trying to reload the gojs one more time. Is there a way to avoid this programmatically or using gojs. I won’t be able to debug the mxui.js because its Mendix framework script and don’t have access to it

Not easily, but I haven’t investigated this. Maybe set window.go = undefined; after the first load and before the second load.

its not letting me assign undefined. I am getting following error Type ‘undefined’ is not assignable to type 'typeof

That’s a TypeScript compiler error, yes? Not a run-time error. So you can cast it.


On further debugging, I am able to see below warning. Looks like the problem is related to creating the same object in the global scope multiple times. do you have any recommendation to fix this issue?

WARNING: a `go` object on the root object is already defined.  version: 2.2.14, replaced with version: 2.2.14 Error: WARNING: a `go` object on the root object is already defined.  version: 2.2.14, replaced with version: 2.2.14

Don’t load the GoJS library multiple times. Not only is that wasteful in terms of bandwidth and processing time and memory usage, but it may also result in some obscure errors when any objects created before the second load interact with any objects created after the second load.

I understand but these are two separate widgets and they are built and packaged separately. I think the Mendix widget framework automatically loads all the npm packages that are part of the widget when each widget is loaded on a page. I don’t have a control over it. This issue is not coming with any other npm packages that are used in the widget. Do you have any recommendations or workarounds on how to avoid this multiple load issues?

Well, it is a warning, not an error, and it’s not likely to cause any problems, so I suggest you live with it.

But the 2nd widget is not rendering. You mentioned above to typecast window.go to some type. What should I type cast it as?

any, I suppose.

What does Mendix support say the problem is? Why isn’t there an error message?
What do they suggest for a fix or work-around?

Here is the response I got but no workaround yet.

This is likely related to each pluggable widget containing its own copy of the GO Framework

If you comment out the first kind of widget using a diagram, does the second kind of widget work by itself, even if there are multiple copies of it? I would want to confirm that it’s the presence of both kinds of widgets together at the same time that causes the error.

Ah, it turns out that when we detect the repeated load, we do throw an Error. If the loading system doesn’t handle that well, I can see how that would cause a problem. We’ll consider changing it to just call console.log with that warning message in the next release.

one possible workaround is, when you can first use GoJS in your widget code space, write:

var go = window.go;
window.go = undefined; // release the library from the global object

This will let GoJS load twice without problems, and still let you use it locally, in that code block. There may be other considerations, however.

Thanks its working