question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Cannot read property "time" of undefined error!

See original GitHub issue

Hey there! I followed your “long version” overview of the SI, really great video btw, presents concepts very well. The problem comes from the server reconciliation function section. The responsible code line in my app, which triggers the error is:

const playerSnapshot = playerVault.get( serverSnapshot.time, true );

When running my game locally, a couple of minutes after start (at random time), the lib crashes with an error:

image

Looking deeper it points to the Vault.get() method:

image

Turns out the problem actually lies in line 23:

var snaps = { older: sorted[i], newer: sorted[i - 1] };

I am running into a situation where if (snap.time <= time) { is passed as true, when the for loop is on it’s first iteration. Then the method tries to access the sorted array with a negative index of -1, returning undefined. Since snaps.newer is undefined, it throws an error when it tries to access snaps.newer.time.

To fix this I patched the function with an if check after if (closest) {:

if (closest) {
  if(snaps.newer == undefined) return;

Since I check whether the returned snapshot is undefined before continuing, my server reconciliation just returns for that particular frame. I checked with a console log, and Im fine, as it doesn’t happen that often - around 3 times per 10 minutes. I dont know the inner workings of networking so this is very unpredictable to me and may be hard to reproduce the bug.

I dont know the library well, so I am not sure why this happens, or why does it happen so rarely. Should there be such an if check, like I’ve implemented? What do you think?

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
DolphinIQcommented, Jan 16, 2021

It’s good, but I’d prefer to use a non optional chaining solution, just to be on the safe side of bundlers and transpilers.

0reactions
yandeucommented, Jan 30, 2021

Don’t worry about that. The code will automatically be transpiled by the TypeScript compiler. The final code will look like this:

if (closest) {
    var older = Math.abs(time - snaps.older.time);
    var newer = Math.abs(time - ((_a = snaps.newer) === null || _a === void 0 ? void 0 : _a.time));
    if (isNaN(newer))
        return snaps.older;
    else if (newer <= older)
        return snaps.older;
    else
        return snaps.newer;
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

TypeError: Cannot read property 'time' of undefined
My variable creating this error while running with node.js. My variable is below. How can I describe my variable correctly ? let allDevices...
Read more >
Cannot Read Property of Undefined in JavaScript - Rollbar
TypeError : Cannot read property of undefined occurs when a property is read or a function is called on an undefined variable.
Read more >
Uncaught TypeError: Cannot read property of undefined In
JavaScript TypeError is thrown when an operand or argument passed to a function is incompatible with the type expected by that operator or...
Read more >
How to Prevent the Error: Cannot Read Property '0' of Undefined
If a property of an object is an array, it's possible to accidentally misspell the key or try to access the array through...
Read more >
How to Avoid the Infamous "Cannot read properties of ... - Bitovi
That error message is telling you the function is returning undefined implicitly, but its return type does not include undefined in it. Awesome!...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found