# Bug? StorageWrite not working as expected

**URL:** https://forum.heroiclabs.com/t/bug-storagewrite-not-working-as-expected/4381
**Category:** Runtime Framework
**Tags:** typescript
**Created:** [September 24, 2023, 12:26pm UTC](https://forum.heroiclabs.com/t/bug-storagewrite-not-working-as-expected/4381 "2023-09-24T12:26:44Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![dela](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.heroiclabs.com/dela/32/1489_2.png) [@dela](https://forum.heroiclabs.com/u/dela)
#### Post date: [September 24, 2023, 12:26pm UTC](https://forum.heroiclabs.com/t/bug-storagewrite-not-working-as-expected/4381/1 "2023-09-24T12:26:44Z")

</div>

Two code examples. Both should work, but only B does.

I try to “create or update” an object in storage engine:

```typescript
  let version = undefined; // ...
  const saveItem = {
    collection: COLLECTION_XS,
    key: req.id,
    userId: ctx.userId,
    value: x,
    version: version,
    permissionRead: 1,
    permissionWrite: 0
  } as nkruntime.StorageWriteRequest;
  const ack = nk.storageWrite([saveItem]);
  // TypeError: expects 'version' value to be a string at github.com/heroiclabs/nakama/v3/server.(*runtimeJavascriptNakamaModule).storageWrite.func1 (native)

```

```typescript
  let version = undefined;
  const saveItem = {
    collection: COLLECTION_XS,
    key: req.id,
    userId: ctx.userId,
    value: x,
    // version: version, // omited
    permissionRead: 1,
    permissionWrite: 0
  } as nkruntime.StorageWriteRequest;
  const ack = nk.storageWrite([saveItem]);
  // Works

```

So in order to actually create an object, i have to branch my code so in the create aspect i do not supply a version property? Your API defines `nkruntime.StorageWriteRequest->version` to be undefined, but then in the execution it fails. Thats a clear contradiction…

1. Versions: I was on Nakama 3.14, upgraded to latest (went through a lot of pain upgrading cockroach; the previous cockroach version is completly uncompatible with newer nakama builds, you get very strange errors there) but the same behavior on the new version.
2. Server Framework Runtime language (If relevant) TS

I am having a workaround thanks to lodash’s omitBy function, which removes all undefined values. I thought i leave this here for others.

```typescript
  const saveItem = _omitBy({ ... }, _isUndefined) as unknown as nkruntime.StorageWriteRequest;
  const ack = nk.storageWrite([saveItem]);

```

As said, this is an error in nakama-common type declaration and needs a fix. I don’t see where in the code exactly this is done so i am not sure where to even start debugging (i’m no Go dev)

---

<div class="post-metadata">

### Author: ![mofirouz](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.heroiclabs.com/mofirouz/32/1523_2.png) [@mofirouz](https://forum.heroiclabs.com/u/mofirouz)
#### Post date: [September 25, 2023, 7:43am UTC](https://forum.heroiclabs.com/t/bug-storagewrite-not-working-as-expected/4381/2 "2023-09-25T07:43:59Z")

</div>

If you pass in a `version` during write, you are telling the server to do a “conditional write” - see this:

> **[Collections](https://heroiclabs.com/docs/nakama/concepts/storage/collections/#conditional-writes)**
>
> Nakama incorporates a storage engine for project-specific data, such as user accounts. The storage engine design is optimized for object ownership, access permissions, and batch operations. Data is stored in collections with one or more objects which...

---

<div class="post-metadata">

### Author: ![dela](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.heroiclabs.com/dela/32/1489_2.png) [@dela](https://forum.heroiclabs.com/u/dela)
#### Post date: [September 25, 2023, 2:35pm UTC](https://forum.heroiclabs.com/t/bug-storagewrite-not-working-as-expected/4381/3 "2023-09-25T14:35:14Z")

</div>

i understand that. It’s a problem with your type definitions!

nkruntime.StorageWriteRequest allows “version” to be undefined (which is correct). But if set and it’s value is undefined, the code fails at runtime with said error:  
`TypeError: expects 'version' value to be a string at github.com/heroiclabs/nakama/v3/server.(*runtimeJavascriptNakamaModule).storageWrite.func1 (native)`

---

<div class="post-metadata">

### Author: ![dela](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.heroiclabs.com/dela/32/1489_2.png) [@dela](https://forum.heroiclabs.com/u/dela)
#### Post date: [October 3, 2023, 7:48pm UTC](https://forum.heroiclabs.com/t/bug-storagewrite-not-working-as-expected/4381/4 "2023-10-03T19:48:43Z")

</div>

@mofirouz if you tell me where the code of `nkruntime.StorageWriteRequest` is, i may be able to open a pull request to fix it…

---

<div class="post-metadata">

### Author: ![sesposito](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.heroiclabs.com/sesposito/32/2594_2.png) [@sesposito](https://forum.heroiclabs.com/u/sesposito)
#### Post date: [October 4, 2023, 9:17am UTC](https://forum.heroiclabs.com/t/bug-storagewrite-not-working-as-expected/4381/5 "2023-10-04T09:17:04Z")

</div>

@dela I’m not sure I understand the issue, the [TS definition](https://github.com/heroiclabs/nakama-common/blob/master/index.d.ts#L2651) looks correct, the `version` field is optional, so if set it must be a string otherwise omit it from the object altogether.

---

<div class="post-metadata">

### Author: ![dela](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.heroiclabs.com/dela/32/1489_2.png) [@dela](https://forum.heroiclabs.com/u/dela)
#### Post date: [October 4, 2023, 9:30am UTC](https://forum.heroiclabs.com/t/bug-storagewrite-not-working-as-expected/4381/6 "2023-10-04T09:30:16Z")

</div>

“optional” (with the ‘?’ sign) meaning it may be “undefined”. Which is so far so good. But the Go code does not allow “undefined” and will throw a TypeError (see my first post). Yet it does allow “null”. So there’s the difference!

---

<div class="post-metadata">

### Author: ![sesposito](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.heroiclabs.com/sesposito/32/2594_2.png) [@sesposito](https://forum.heroiclabs.com/u/sesposito)
#### Post date: [October 4, 2023, 10:40am UTC](https://forum.heroiclabs.com/t/bug-storagewrite-not-working-as-expected/4381/7 "2023-10-04T10:40:47Z")

</div>

You’re correct, I was not aware the `?` operand would also allow the value itself to be `undefined` - a [PR with a fix](https://github.com/heroiclabs/nakama/pull/1094) has been opened, will be part of the next release.

---

<div class="post-metadata">

### Author: ![dela](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.heroiclabs.com/dela/32/1489_2.png) [@dela](https://forum.heroiclabs.com/u/dela)
#### Post date: [October 4, 2023, 11:06am UTC](https://forum.heroiclabs.com/t/bug-storagewrite-not-working-as-expected/4381/8 "2023-10-04T11:06:13Z")

</div>

Thank you!

Usually, having a variable “undefined” in Javascript is the same as not having it in the object at all (“optional” so to say).  
Seems that Go does treat that different. “null” and “undefined” are two separate things in JS/TS. I dont know how Go (i.e. the serializer) handles that internally.
