I’m trying to update a Speckle model (from Navisworks) in TS and send the updated model back to Speckle using @speckle/objectsender.
I pull the model using @speckle/objectloader updates a few properties and want to send it back again.
On sending it back I get this (very descriptive) error: TypeError: l is null, which is thrown on line 107 of the Serializer.ts file. Which looks like this:
From the syntax of your saving function I am assuming this code is running in React. React is very smart on optimising code and functions that get triggered on users interaction. The TypeError: [x] is null is a common problem that happens when the DOM has not yet been mounted, eg. React runs on a virtual DOM until a very late point.
I would bet that React is packing your function to run at a later moment, and during this packing is probably putting some magic properties (l = null) on your speckleStateData object, who knows why.
I would start by splitting your code between pure javascript, and the React part. One way that I think could work is by having a pure JS object to control the state, and only inside the handleSave function transforming the JS obj to your Base Speckle TS class.
So handleSave is a callback for a button onClick event, which in principle means that whatever happens inside handleSave no longer has anything to do with the DOM. And handleSave is fired correctly.
Inspecting (debugging/logging) speckleStateData just before passing it to send reveals that the data looks correct (and Speckle like).
I’ve tried moving the handleSave out of the component, but the error stays the same.
I thought about what you wrote with transforming the JS obj to your Base Speckle TS class.
So I tried to play around with my data a bit and that affects the error and when it occurs in the #traverse() loop.
I’ll test a bit more and see if something comes up
So I investigated the issue more digging into my data.
The problem arise because I have a key in my data with a value of null.
Hence TypeError l is null. (the key is not called l, so thank you for mimifiying my data JavaScript…)
Anyways, I looked into the objectsender package and the test cases doesn’t include any null values. If you add nothing: null to generateTestObject in examples/browser/main.ts and run the test it fails, with the error Value is null.
]),
'@(10)chunkedArr': times(100, () => 42),
some: new RandomJoe(),
nothing: null // line 121
})
If you update line 60 of utils/Serializer.ts to catch null values everything works well again.
// 1. primitives (numbers, bools, strings)
if (value === null || typeof value !== 'object') { // line 60
traversed[propKey] = value
continue
}
Should I create a PR or is there someone from the Speckle team that want to look at it more?