clockworklabs/SpacetimeDB · error · TypeError
wrong number of elements
Error message
wrong number of elements
What it means
For a UNIQUE index spanning multiple columns, the runtime builds a `find` that takes an array of column values and checks `colVal.length !== numColumns` before serializing the point lookup. Passing fewer or more elements than the index has columns throws `wrong number of elements`. This is the multi-column counterpart of a single-value lookup; the array must match the index's column list exactly, in definition order.
Solutions
- Pass exactly one value per indexed column, in the same order the columns are declared in the index: for an index on `[a, b]` call `idx.find([aVal, bVal])`.
- After editing an index definition, grep every `find`/`delete` on that index and update the arrays.
- If you only know a prefix, that is a range scan, not a point lookup — use `iter`/range APIs instead of `find`.
- Add a length assertion helper in your code that ties the array to the index columns at development time.
Example fix
// before — index on (tenantId, email) const user = byTenantEmail.find([tenantId]); // TypeError: wrong number of elements // after const user = byTenantEmail.find([tenantId, email]);
Defensive patterns
Strategy: validation
Validate before calling
// Centralize composite-key construction next to the index definition.
const TENANT_EMAIL_COLS = 2; // index on (tenantId, email)
function tenantEmailKey(tenantId: bigint, email: string): unknown[] {
const key = [tenantId, email];
if (key.length !== TENANT_EMAIL_COLS) throw new Error('bad key arity');
return key;
}
// byTenantEmail.find(tenantEmailKey(tenantId, email)); Type guard
type CompositeKey2<A, B> = [A, B]; // tuple types fix arity at compile time
function isCompositeKey2<A, B>(k: unknown[]): k is CompositeKey2<A, B> { return k.length === 2; } Try / catch
try { const row = idx.find(key); } catch (e) { if (e instanceof TypeError && e.message === 'wrong number of elements') { /* log expected arity vs provided length, fall back to full scan + filter */ } else throw e; } Prevention
- Type lookup keys as tuples whose length equals the index column count.
- Never spread dynamic arrays directly into find(); construct them in one helper.
- When an index definition changes, grep for its name and re-check every call site's array length.
When it happens
Trigger: Calling `table.<multiColUniqueIndex>.find([...])` with the wrong array length: `idx.find([1])` on a two-column unique index, or `idx.find([1, 2, 3])` on a two-column index; reordering code after adding/removing a column from the index without updating call sites.
Common situations: Adding a column to a composite unique index (e.g. `(tenantId, email)` -> `(tenantId, email, region)`) and forgetting one lookup site; copy-pasting a `find` from a single-column index and not extending the array; conditional lookups that sometimes omit trailing columns.
Related errors
- too many elements
- When providing a name, you must also provide the object.
- AddIndex: index ` ` not found in new module def
- AddIndex: ` ` not found in new module def
- ERROR: Failed to read from BytesSource
AI-assisted analysis of clockworklabs/SpacetimeDB@eddf9f5014 (2026-08-20).
Data as JSON: /api/errors/6dd124c5fbafea93.
Report an issue: GitHub.
Appendix: source
Thrown at crates/bindings-typescript/src/server/runtime.ts:1286
BINARY_WRITER.reset(buf);
serializeRow(BINARY_WRITER, row);
sys.datastore_update_bsatn(
table_id,
index_id,
buf.buffer,
BINARY_WRITER.offset
);
integrateGeneratedColumns?.(row, buf.view);
return row;
};
}
index = base as UniqueIndex<any, any>;
} else if (isUnique) {
// numColumns != 1, unique index
const base = {
find: (colVal: IndexVal<any, any>): RowType<any> | null => {
if (colVal.length !== numColumns) {
throw new TypeError('wrong number of elements');
}
const buf = LEAF_BUF;
const point_len = serializePoint(buf, colVal);
const iter_id = sys.datastore_index_scan_point_bsatn(
index_id,
buf.buffer,
point_len
);
return tableIterateOne(iter_id, deserializeRow);
},
delete: (colVal: IndexVal<any, any>): boolean => {
if (colVal.length !== numColumns)
throw new TypeError('wrong number of elements');
const buf = LEAF_BUF;
const point_len = serializePoint(buf, colVal);
const num = sys.datastore_delete_by_index_scan_point_bsatn(
index_id,View on GitHub (pinned to eddf9f5014)