1

我一直在开发一个反应应用程序。只是一个简单的应用程序,它有一个功能,我可以检查列表中项目的切换/切换状态。

在 utils.js 我有

export const partial = (fn, ...args) => fn.bind(null, ...args)
const _pipe = (f, g) => (...args) => g(f(...args))
export const pipe = (...fns) => fns.reduce(_pipe)

但是在使用 utils 时 App.js 中存在问题:

   const getToggledTodo = pipe(findById, toggleCompleted) 

助手的进口似乎很好:

import {pipe, partial} from  './lib/utils'

import {addTodo, generateId, findById, toggleCompleted, 
        updateTodo, removeTodo, filterTodos} from './lib/todoHelpers'

仍然,该应用程序抱怨

Uncaught TypeError: Cannot read property 'find' of undefined

做控制台我得到:

f2: function () {
        return g(f.apply(undefined, arguments));
      }

我看着:

   at findById (todoHelpers.js:15)
    at utils.js:10

    at Object.executeOnChange (LinkedValueUtils.js:132)

在我看来,未定义来自最后一行的linkedValue文件:

  executeOnChange: function (inputProps, event) {
    if (inputProps.valueLink) {
      _assertValueLink(inputProps);
      return inputProps.valueLink.requestChange(event.target.value);
    } else if (inputProps.checkedLink) {
      _assertCheckedLink(inputProps);
      return inputProps.checkedLink.requestChange(event.target.checked);
    } else if (inputProps.onChange) {
      return inputProps.onChange.call(undefined, event);
    }
  }
};

不确定 .apply 和 .call 在这里如何相互关联,在我看来,我在某处遗漏了一个论点。最终目标是在数据库中更新状态完成/未完成,并在 UI 中显示一条消息,说明该项目实际上已更新。

有趣的事实:如果我在 App.js 中对一些类似的结构化对象进行硬编码并在内存中使用它来更改状态,则不会显示错误... o_O。它仅在尝试连接到“db”时出现,当然这仍然是一个模拟。不知道它是否相关,但我认为值得一提。使用 json-server 模拟 db 对象。

所以我的问题是:如何调试这个错误?有人可以帮助我了解一下 apply 和 call 与此错误的关系。

任何朝着正确方向的指针都会非常有帮助,非常感谢。

4

0 回答 0