当前位置: 首页 > 知识库问答 >
问题:

第0行:分析错误:无法读取未定义的属性“map”

缑智敏
2023-03-14

在我创建了useMutation组件并将其导出到index.ts中之后,这个错误出现了,我不知道如何消除这个错误。

Below is my package.json
    {
    "name": "tinyhouse_client",
    "version": "0.1.0",
    "private": true,
      "dependencies": {
      "@testing-library/jest-dom": "^4.2.4",
      "@testing-library/react": "^9.3.2",
      "@testing-library/user-event": "^7.1.2",
      "@types/jest": "^24.0.0",
      "@types/node": "^12.0.0",
      "@types/react": "^16.9.35",
      "@types/react-dom": "^16.9.0",
      "@typescript-eslint/parser": "^3.0.2",
      "react": "^16.13.1",
      "react-dom": "^16.13.1",
      "react-scripts": "3.4.1",
      "typescript": "~2.23.0"
      },
      "resolutions": {
     "@typescript-eslint/eslint-plugin": "^2.23.0",
     "@typescript-eslint/parser": "^2.23.0",
     "@typescript-eslint/typescript-estree": "^2.23.0"
     },
     "scripts": {
     "start": "react-scripts start",
     " build": "react-scripts build",
     "test": "react-scripts test",
     "eject": "react-scripts eject"
     },
     "eslintConfig": {
     "extends": "react-app"
     },
     "browserslist": {
     "production": [
      ">0.2%",
      "not dead",
      "not op_mini all"
    ],
      "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version"
     ]
     },
     **strong text** "proxy": "http://localhost:9000"
      }
Below is my index.ts
    export * from './server';
    export * from './useQuery';
    export * from './useMutation';
And my useMutation.ts
    import { useState } from 'react';
    import { server } from './server';

    interface State<TData> {
    data: TData | null;
    loading: boolean; 
    error: boolean;
    }

    type MutationTuple<TData, TVariables> = [
    (variables?: TVariables | undefined) => Promise<void>,
    State<TData>
    ];

    export const useMutation = <TData = any, TVariables = any>(
    query: string
    ): MutationTuple<TData, TVariables> => { 
    const [state, setState] = useState<State<TData>>({
    data: null,
    loading: false,
    error: false,
    })

    const fetch = async (variables?: TVariables) => {
    try {
      setState({ data: null, loading: true, error: false });

      const { data, errors } = await server.fetch<TData, TVariables>({ query, variables });
      if (errors && errors.length) {
        throw new Error(errors[0].message);
      }

      setState({ data, loading: false, error: false });
    } catch (err) {
      setState({ data: null, loading: false, error: true });
      throw console.error(err);
    }
   }

   return [fetch, state];
};

共有1个答案

贺宜修
2023-03-14

编辑:正如Meng-Yuan Huang所指出的,此问题不再发生在react-scripts@^4.0.1

出现此错误是因为react-scripts直接依赖于@typescript-eslint/parser@typescript-eslint/eslint-plugin的2.xx范围。

您可以通过在package.json中添加一个resolutions字段来解决这个问题,如下所示:

"resolutions": {
  "**/@typescript-eslint/eslint-plugin": "^4.1.1",
  "**/@typescript-eslint/parser": "^4.1.1"
}

NPM用户:将上面的resolutions字段添加到package.json中,但是使用npx npm-force-resolutions更新package-lock.json中的包版本。

纱线用户:你不需要做其他的事情。有关更多信息,请参见选择性依赖关系解决方案。

注意:如果使用的是monorepo/yarn工作区,resolutions字段必须位于顶层的package.json中。

 类似资料:
  • 我正在使用webpack,浏览器中出现以下错误: 编译时没有错误或警告。 validator.js的第15行如下所示:

  • 这是我的功能。显示解析错误(data.ops[0])

  • 问题内容: 我是Reactjs的新手。我正在尝试做一个非常简单的事情:当用户在文本区域内更改文本时,在render函数中更新div。有什么建议? 问题答案: 您应该绑定该函数。您收到此错误的原因是,在handleChange函数中,键盘操作未引用React类的上下文,因此您需要绑定该函数。 看到这个答案

  • 我搜索了这个网站,发现了类似的问题,但没有解决我的问题。当我滚动到一个div时,我试图使它固定在屏幕顶部。但我一直在犯错误: "(index): 59未捕获类型错误:无法读取未定义的属性'top'at(index): 59" 我还在学习jQuery,不能解决这个问题。

  • 问题内容: 我正在使用jQuery将表单字段发布到PHP文件,该文件仅根据是否工作而返回1/0。 代码摘录: 但是,每次成功(html == 1)时,控制台都会引发错误“未捕获的TypeError:无法读取未定义的属性’defaultView’”,并且警报永远不会发生…? Google似乎没有关于此错误和jQuery的大量信息,谁知道原因? 问题答案: 这是因为以前不是您要处理的内容,现在是jQu

  • 我的代码: 错误: 未处理PromisejectionWarning:TypeError:无法读取未定义的未处理PromisejectionWarning:未处理的promise拒绝的属性“forEach”。此错误源于在没有catch块的情况下抛出异步函数的内部,或者拒绝使用未处理的promise。catch()。(拒绝id:1)(节点:7188)[DEP0018]弃用警告:未处理的promise