Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

getOrCreateContainer throws an exception when the existing value is null #575

Closed
optimalstrategy opened this issue Dec 9, 2024 · 0 comments · Fixed by #576
Closed

getOrCreateContainer throws an exception when the existing value is null #575

optimalstrategy opened this issue Dec 9, 2024 · 0 comments · Fixed by #576

Comments

@optimalstrategy
Copy link

optimalstrategy commented Dec 9, 2024

One of the documents we're using Loro with has a value that may be null or a container like LoroMap. When the value is null and we try to initialize it with a container using getOrCreateContainer, Loro throws an exception.

It is possible to avoid this error by first checking if the stored value is null and using setContainer in that case. However, I think it would be more ergonomic if getOrCreateContainer treated null values the same way as the field not being initialized at all.

  • loro-crdt version: 1.1.4

Example

import { LoroDoc, LoroMap } from "loro-crdt";

const doc = new LoroDoc();
const root = doc.getMap("root");

// No key -- ok
root.getOrCreateContainer("key", new LoroMap());
doc.commit();
console.log(doc.toJSON());

// Set to null  -- ok
root.set("key", null);
doc.commit();
console.log(doc.toJSON());

// Key is null, create a container
try {
    root.getOrCreateContainer("key", new LoroMap());  
} catch (e) {
    console.error(e);  // Invalid argument (Expected value type Map but found Value(Null))
}

// Avoids the error but defeats the purpose of getOrCreateContainer
let inner = root.get("key");
if (!inner) {
    inner = root.setContainer("key", new LoroMap());
}
console.log(doc.toJSON())
zxch3n added a commit that referenced this issue Dec 10, 2024
* fix: getOrCreateContainer should not throw if value is null

resolve #575

* chore: update changeset release info
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant