fast icon indicating copy to clipboard operation
fast copied to clipboard

Unhandled `TypeError: currentParams.get is not a function` in `QueryString.parse()` when parsing bracket / nested query strings

Open codeCraft-Ritik opened this issue 1 month ago • 0 comments


🐛 Bug Report

Calling QueryString.parse() on query strings with bracketed keys (such as filter[category]=electronics, user[name]=John, or items[]=1) throws an unhandled TypeError: currentParams.get is not a function.

💻 Repro or Code Sample

import { QueryString } from "@microsoft/fast-router";

// 1. Build a complex query string using FAST's own builder
const builtQuery = QueryString.build({
    filter: { category: "hardware", inStock: "true" } as any,
});
console.log("Built query string:", builtQuery);
// Output: "filter[category]=hardware&filter[inStock]=true"

// 2. Parse the generated query string back into an object
try {
    const parsed = QueryString.parse(builtQuery);
    console.log("Parsed result:", parsed);
} catch (error) {
    console.error("Runtime exception:", error);
}

Minimal standalone reproduction:

import { QueryString } from "@microsoft/fast-router";

// Direct parse of standard bracketed parameters
QueryString.parse("user[name]=Alice&user[role]=admin");

🤔 Expected Behavior

QueryString.parse() should successfully parse nested key parameters into structured objects or arrays without crashing, allowing seamless round-tripping with QueryString.build().

For example, QueryString.parse("filter[category]=hardware") should return:

{
  "filter": {
    "category": "hardware"
  }
}

😯 Current Behavior

A runtime TypeError is thrown immediately, crashing route transitions and query parameter deserialization:

TypeError: currentParams.get is not a function
    at parseComplexParam (packages/fast-router/src/query-string.ts:68:32)
    at Object.parse (packages/fast-router/src/query-string.ts:174:21)

Root Cause Analysis

In packages/fast-router/src/query-string.ts, line 137 initializes queryParams as a plain JavaScript object:

const queryParams = {} as Record<string, any>;

However, in parseComplexParam (lines 54–78), the implementation attempts to invoke Map-like .get() and .set() methods directly on currentParams:

function parseComplexParam(
    queryParams: Record<string, any>,
    keys: string[],
    value: any
): void {
    let currentParams: Record<string, any> = queryParams;
    const keysLastIndex = keys.length - 1;

    for (let j = 0; j <= keysLastIndex; j++) {
        const key = keys[j] === "" ? currentParams.length : keys[j];
        if (j < keysLastIndex) {
            const prevValue =
                !currentParams.get(key) || typeof currentParams.get(key) === "object"
                    ? currentParams.get(key)
                    : [currentParams.get(key)];
            currentParams.set(key, prevValue || (isNaN(keys[j + 1] as any) ? {} : []));
            currentParams = currentParams.get(key);
        } else {
            currentParams.set(key, value);
            currentParams = currentParams.get(key);
        }
    }
}

Because currentParams is a standard JavaScript Object (or Array), .get() and .set() do not exist, triggering an immediate TypeError. Additionally, line 63 accesses currentParams.length for empty bracket keys [], which evaluates to undefined when currentParams is an object.

💁 Possible Solution

Refactor parseComplexParam to use standard JavaScript property access ([] indexing) and prototype checks:

function parseComplexParam(
    queryParams: Record<string, any>,
    keys: string[],
    value: any
): void {
    let currentParams: any = queryParams;
    const keysLastIndex = keys.length - 1;

    for (let j = 0; j <= keysLastIndex; j++) {
        let key = keys[j];
        if (key === "") {
            key = Array.isArray(currentParams) ? currentParams.length.toString() : "0";
        }

        if (j < keysLastIndex) {
            const nextKeyIsNumeric = !Number.isNaN(Number(keys[j + 1])) && keys[j + 1] !== "";
            if (currentParams[key] === undefined || typeof currentParams[key] !== "object") {
                currentParams[key] = nextKeyIsNumeric ? [] : {};
            }
            currentParams = currentParams[key];
        } else {
            if (Array.isArray(currentParams)) {
                currentParams.push(value);
            } else {
                currentParams[key] = value;
            }
        }
    }
}

🔦 Context

When building single-page applications with @microsoft/fast-router, passing structured filter states, pagination dictionaries, or array selections via URL search parameters is a standard design pattern. Furthermore, QueryString.build() natively constructs bracketed query parameters when receiving nested objects. Because QueryString.parse() crashes when parsing this syntax, round-tripping query state between navigation pushes is completely broken.

🌍 Your Environment

  • OS & Device: Windows 11 / macOS / Linux on PC
  • Browser: Microsoft Edge, Google Chrome, Mozilla Firefox, Apple Safari
  • Version: @microsoft/fast-router 1.8.0 / @microsoft/fast 1.8.0

codeCraft-Ritik avatar Oct 12 '26 04:10 codeCraft-Ritik