Pushing items in newly created arrays in TypeScript but arrays remain empty

Hello,

Our team has noticed this quirk a couple times now when writing server logic in TypeScript for Nakama where sometimes if an array within a TypeScript object is created and then pushed to nothing actually gets pushed onto the array. If the array already exists within the object then pushing to it works as expected. The issue only happens if the array is newly created.

This has caused us some confusion because on the surface the code looks like it should work just fine and so when the functionality isn’t behaving as expected it is harder to track down where the issue is. We have work arounds which aren’t a problem but we’re just trying to understand why our code doesn’t work as expected.

  1. Nakama server framework v3.21
  2. Server Framework Runtime language: TypeScript

Short examples of what I’m talking about:

// Works
let inventory = userData.rewards.inventory ?? []
inventory.push(item)
userData.rewards.inventory = inventory

// Doesn't work, array is empty after "push"
userData.rewards.inventory ??= []
userData.rewards.inventory.push(item)
		
// Doesn't work, array is empty after "push"
if (!userData.rewards.inventory) {
	userData.rewards.inventory = []
}
userData.rewards.inventory.push(refiner)

I wrote a few quick Jest test cases and running those using the same logic in the above snippet the code works just fine(items are pushed onto the arrays).

Is this something that is unique to the Goja framework and/or Go? I searched through Goja’s Github issues but couldn’t find any related cases there.

Thank you for your time!

1 Like

Hello @chris1,

This is a quirk of Goja, under some circumstances (if the underlying JS object is backed by a Go structure such as a slice) some operations such as pushing to it won’t actually apply to the backing structure.

We do our best to go around these issues to provide the expected JS behavior but we may have missed something.

Did you observe this in an RPC or Match Handlers?

Besides the above snippets, could you please provide a small example or the bundled code of one that we could use to reproduce it and see if we can fix it?

Best.