Skip to content

fix: resolve logic bug and mutable default argument in getUserDataForFid - #553

Open
dajiaohuang wants to merge 1 commit into
framesjs:mainfrom
dajiaohuang:fix/logic-bug-in-getUserDataForFid
Open

fix: resolve logic bug and mutable default argument in getUserDataForFid#553
dajiaohuang wants to merge 1 commit into
framesjs:mainfrom
dajiaohuang:fix/logic-bug-in-getUserDataForFid

Conversation

@dajiaohuang

Copy link
Copy Markdown

Summary

修复了 getUserDataForFid 函数中的两个问题:

Bug 1: 逻辑错误

原代码中 if/else 的两个分支执行完全相同的操作:

if (foundValue && foundValue.timestamp < timestamp) {
  acc[type] = { value, timestamp };
} else {
  acc[type] = { value, timestamp }; // 两个分支完全相同
}

这会导致当找到的值更新时不会覆盖旧值,或者无论条件如何都覆盖。正确的逻辑应该是只在找到的值比当前值旧时才更新。

Bug 2: 可变默认参数

JavaScript 中使用可变对象作为默认参数是一个反模式,因为默认对象在所有调用中共享。原代码:

hubRequestOptions = {
  headers: {
    api_key: DEFAULT_HUB_API_KEY,
  },
},

会在所有调用中共享同一个对象,如果调用者修改了它,会影响后续调用。

Fix

  • 移除了冗余的 else 分支
  • 创建新的 requestOptions 对象,每次调用时都是fresh的
  • 正确合并用户提供的 hubRequestOptions 和默认的 api_key header

- Remove redundant else branch that was overwriting acc[type] with the same value
- Fix mutable default argument by creating requestOptions object fresh each time
- Properly merge user-provided hubRequestOptions with default api_key header
@vercel

vercel Bot commented Sep 2, 2026

Copy link
Copy Markdown

@dajiaohuang is attempting to deploy a commit to the Discove Team on Vercel.

A member of the Team first needs to authorize it.

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.

1 participant