krisk/Fuse · error · Error
Invalid value for key ${key}
Error message
Invalid value for key ${key} What it means
When parsing a logical/field query, each key's value must be a valid query shape for that key. If a value cannot be compiled into a valid field query (wrong type for the operator or key path), Fuse throws `Invalid value for key ${key}` naming the offending key.
Source
Thrown at src/core/queryParser.ts:127
if (isObjectLike(value) && !isArray(value)) {
const obj: ParsedLeaf = {
keyId: createKeyId(key),
fieldQuery: value
}
if (auto) {
const compile = getObjectCompiler()
if (!compile) {
throw new Error(ErrorMsg.OBJECT_QUERY_UNAVAILABLE)
}
const keyPath = isArray(key) ? key.join('.') : String(key)
obj.searcher = compile(value, keyPath, options)
}
return obj
}
throw new Error(ErrorMsg.LOGICAL_SEARCH_INVALID_QUERY_FOR_KEY(key))
}
const node: ParsedOperator = {
children: [],
operator: keys[0]
}
keys.forEach((key) => {
const value = query[key]
if (isArray(value)) {
value.forEach((item: any) => {
node.children.push(next(item))
})
}
})
return nodeView on GitHub (pinned to edf2fb608e)
Solutions
- Ensure each field's value is a string pattern or a valid extended-search operator object ({ $eq: ... }, { $contains: ... }).
- Validate/log the query object before calling search; guard builders against undefined values.
- Enable extended search so operator objects are recognized instead of rejected.
Example fix
// before
fuse.search({ $and: [{ title: undefined }] })
// after
const title = getTitle() ?? ''
fuse.search({ $and: [{ title }] }) Defensive patterns
Strategy: validation
Validate before calling
const isValidFieldValue = (v) =>
typeof v === 'string' || (v !== null && typeof v === 'object' && !Array.isArray(v))
for (const [key, value] of Object.entries(query)) {
if (!isValidFieldValue(value)) throw new TypeError(`Invalid value for key ${key}`)
}
fuse.search(query) Type guard
const isValidFieldValue = (v) => typeof v === 'string' || (v !== null && typeof v === 'object' && !Array.isArray(v))
Try / catch
try {
return fuse.search(query)
} catch (e) {
if (e.message.startsWith('Invalid value for key')) {
console.warn('Dropping malformed logical query:', e.message)
return []
}
throw e
} Prevention
- Sanitize programmatically built queries (no undefined/null values).
- Use only string patterns or valid $-operator objects as field values.
- Unit-test query builders with edge-case inputs.
When it happens
Trigger: fuse.search({ $and: [{ title: 42 }] }) or a field value that is neither a string nor a recognized operator object — e.g. { author: true }, { tags: null }, or a nested structure the parser cannot interpret (often from a programmatic builder producing undefined).
Common situations: Building queries programmatically where a variable ends up undefined/null; typos in operator names leaving unrecognized objects; passing numbers/booleans where string patterns are expected.
Related errors
- Logical search is not available
- Object query syntax is not available in this build
- Invalid field query for key '${key}': ${reason}
AI-assisted analysis of krisk/Fuse@edf2fb608e (2026-09-02).
Data as JSON: /api/errors/f30b1ae32e0070b7.
Report an issue: GitHub.