Skip to content

initModel() 函数中的判断 modelId 值是否为空的逻辑有错误 #182

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
weilong66 opened this issue Apr 30, 2025 · 1 comment
Closed
Labels
bug Something isn't working solved

Comments

@weilong66
Copy link

158行 左右的初始化函数中,会先从 localStorage 中读取modelId,并将其转换为数字类型(number | null)。但如果 localStorage.getItem('modelId') 返回的是 null(即该键不存在),似乎仍然会把它转换成 Number,导致结果会是 NaN 而不是 null
这就会导致 162行 的 if (modelId === null) 判断出错。
我的解决方法是直接去掉 === null ,改为直接判断 modelId 变量的值是否为假值。
原始代码:

(function initModel() {
    let modelId: number | null = Number(localStorage.getItem('modelId'));
    let modelTexturesId: number | null = Number(
      localStorage.getItem('modelTexturesId'),
    );
    if (modelId === null) {
      // 首次访问加载 指定模型 的 指定材质
      modelId = 1; // 模型 ID
      modelTexturesId = 53; // 材质 ID
    }

修改后:

(function initModel() {
    let modelId: number | null = Number(localStorage.getItem('modelId'));
    let modelTexturesId: number | null = Number(
      localStorage.getItem('modelTexturesId'),
    );
    if (!modelId) {
      // 首次访问加载 指定模型 的 指定材质
      modelId = 1; // 模型 ID
      modelTexturesId = 53; // 材质 ID
    }
whats2000 added a commit to whats2000/live2d-widget that referenced this issue Apr 30, 2025
stevenjoezhang added a commit that referenced this issue May 6, 2025
* Fix the missing initWidget

See #176

* Fix the check of invalid model id

See #182

---------

Co-authored-by: Mimi <[email protected]>
@stevenjoezhang
Copy link
Owner

谢谢反馈,已在 #175 中修复,感谢 @whats2000
如果问题仍然存在,欢迎继续回复

@stevenjoezhang stevenjoezhang added bug Something isn't working solved labels May 18, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working solved
Projects
None yet
Development

No branches or pull requests

2 participants