Skip to content

Commit 85553e9

Browse files
committed
Return undefined on inexisting key
1 parent 960765b commit 85553e9

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

src/nested.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,11 @@ export const NestedApi = ({ database }: { database: InternalDatabase }) => {
112112
}
113113
let nested: PossiblyNestedValue = toNested(relevantKeyValues);
114114
for (const k of splitKey(joinedKey)) {
115-
nested = (nested as NestedValue)[k];
115+
try {
116+
nested = (nested as NestedValue)[k];
117+
} catch {
118+
return undefined;
119+
}
116120
}
117121
return nested;
118122
};

test/nested.spec.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,15 @@ describe("Nested Database", () => {
180180
expect(actualAB).to.equal(1);
181181
});
182182

183+
184+
it("get an inexisting nested key", async () => {
185+
await db.put("b/c", "test");
186+
await db.del("b/c");
187+
188+
const actual = await db.get("b/c");
189+
expect(actual).to.be.undefined();
190+
});
191+
183192
it("remove a nested value", async () => {
184193
await db.put(["a/b"], 1);
185194
await db.put("a/c", 2);

0 commit comments

Comments
 (0)