Skip to content

fix: guard against undefined module in ModuleCollection.get() (fix #2164) - #2274

Open
JSap0914 wants to merge 1 commit into
vuejs:mainfrom
JSap0914:fix/module-collection-get-deep-path
Open

fix: guard against undefined module in ModuleCollection.get() (fix #2164)#2274
JSap0914 wants to merge 1 commit into
vuejs:mainfrom
JSap0914:fix/module-collection-get-deep-path

Conversation

@JSap0914

Copy link
Copy Markdown

Problem

store.hasModule(path) throws a TypeError when called with a path that is 3 or more levels deep and no intermediate module exists.

Reproduction:

const store = new Vuex.Store({ /* no nested modules */ })
store.hasModule(["a", "b", "c"]) // TypeError: Cannot read properties of undefined (reading "getChild")

The crash originates in ModuleCollection.get():

get (path) {
  return path.reduce((module, key) => {
    return module.getChild(key)  // throws if module is undefined
  }, this.root)
}

When the first segment is not registered, getChild returns undefined. The next iteration then calls undefined.getChild(...), which throws.

isRegistered() already has a if (parent) guard after calling get(), but that guard is never reached because get() throws first.

This was partially addressed in #1851 for 2-level paths, but the get() method itself was not fixed, so paths of 3+ non-existent segments still crash.

Fix

Short-circuit the reduce when the accumulated module is falsy:

return module && module.getChild(key)

This is a one-character change that makes get() return undefined instead of throwing, allowing the existing guard in isRegistered() to do its job.

Testing

Added a regression test for the 3-level non-existent path case. All 102 existing unit tests continue to pass.

AI-assisted implementation.

…ejs#2164)

When isRegistered() is called with a path whose intermediate segments do
not exist (e.g. ['a', 'b', 'c'] where 'a' is not registered), the
reduce inside get() called .getChild() on undefined, throwing a TypeError.

The fix short-circuits the reduce by returning undefined when the
accumulated module is falsy, matching the existing null-guard already
present in isRegistered() itself.

Added a regression test for the 3-level non-existent path case.
Copilot AI review requested due to automatic review settings June 23, 2026 05:32
@netlify

netlify Bot commented Jun 23, 2026

Copy link
Copy Markdown

Deploy Preview for vuex-docs ready!

Name Link
🔨 Latest commit fa9fce5
🔍 Latest deploy log https://app.netlify.com/projects/vuex-docs/deploys/6a3a1a61e9537d00085a70d4
😎 Deploy Preview https://deploy-preview-2274--vuex-docs.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

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 this pull request may close these issues.

2 participants