ThatOpen / web-ifc-viewer

Graphics engine and toolkit for client applications.
MIT License
947 stars 235 forks source link

"Uncaught (in promise) TypeError: this context is undefined" in prePickIfcItem #114

Closed m1900 closed 2 years ago

m1900 commented 2 years ago

I keep getting the mentioned error in my application based on this post https://ifcjs.github.io/info/blog/Build%20a%20CDE%20in%2030%20minutes/ (I left out the Google Drive part and select an IFC using a simple input element instead). Version of web-ifc-viewer used is 1.0.168.

An extract of my code can be found in my ProtoIfcViewer repo.

m1900 commented 2 years ago

This issue actually belongs to web-ifc-viewer but I don't have the right to transfer it.

simplynail commented 2 years ago

Hi @m1900 , I'm getting similar error when basing my code on "google drive CDE" blog post but for local drive ifc file selection. I'm passing ifcfile = event.target.files[0] to window.webIfcAPI.IFC.loadIfc(ifcfile) (I'm using "test.ifc" file from "examples") and I get:

       Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'prePickIfcItem')
       at prePickIfcItem (main.js:91596:33)
       prePickIfcItem @ main.js:91596
       Error loading IFC. @ main.js:91516
    RuntimeError: null function or function signature mismatch @ main.js:91517
    at web-ifc.wasm:0x3bc26
    at Module.___errno_location (main.js:49936:94)
    at setErrNo (main.js:49475:22)
    at _clock_gettime (main.js:49485:17)
    at web-ifc.wasm:0x25cfb
    at Module.___wasm_call_ctors (main.js:49918:96)
    at func (main.js:49913:15)
    at callRuntimeCallbacks (main.js:45769:19)
    at initRuntime (main.js:45607:15)
    at doRun (main.js:50012:17)
simplynail commented 2 years ago

Hi @m1900 , I was able to solve the problem by swapping web-ifc.wasm file inside my app with the most recent builds (found here https://github.com/IFCjs/web-ifc-viewer/tree/master/example/files - I've copied both web-ifc.wasm and web-ifc-mt.wasm, I'm going through the documentation but have not yet found the purpose for -mt.wasm file).

For some reason the original google-drive-viewer example didn't work for me with original .wasm file from https://github.com/IFCjs/hello-world/tree/main/examples/web-ifc-viewer/google-drive-viewer/wasm

simplynail commented 2 years ago

sorry Guys, my bad - I've noticed that the only thing that swapping .wasm did for me was that the model actually got loaded and shown in the browser, but the Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'prePickIfcItem') (main.js:91596:33) error is still there

agviegas commented 2 years ago

Hi, this is probably because you are binding the prepick function to an event directly, like this:

window.onmousemove = viewerAPI.IFC.selector.prePickIfcItem;

Because the function is executed by the eventhandler, the reference to this is lost. You could try something like this:

window.onmousemove = () => viewerAPI.IFC.selector.prePickIfcItem();

Let me know how it goes!

m1900 commented 2 years ago

Hi Antonio, this solved my problem 😃. It makes sense that this is the way to bind the prepick function.

I think https://github.com/IFCjs/hello-world/blob/main/examples/web-ifc-viewer/google-drive-viewer/main.js needs to be corrected.

simplynail commented 2 years ago

hi, worked for me as well (for what is worth I needed to do window.onmousemove = () => viewer.IFC.selector.prePickIfcItem(); while earlier instantiating viewer as const viewer = new IfcViewerAPI();