简体   繁体   English

错误:无法将未定义或 null 转换为 object

[英]Error: Cannot convert undefined or null to object

I make request to the server using fetch and get response - category list.我使用fetch向服务器发出请求并获取响应 - 类别列表。 This list I display in view- table.这个列表我显示在视图表中。 Near each category on the right I have button Delete .在右侧的每个类别附近,我都有按钮Delete When I click this button appears modal window with two buttons Delete and Cancel .当我单击此按钮时,出现模态 window ,带有两个按钮DeleteCancel I used API method DELETE that delete some category.我使用 API 方法DELETE删除某些类别。

When I click button Delete in modal window当我单击模态 window 中的按钮Delete

My category delete!我的分类删除! It's good!很好!

But in my page appears error:但是在我的页面中出现错误:

TypeError: Cannot convert undefined or null to object类型错误:无法将未定义或 null 转换为 object

Why is this happening?为什么会这样? And how to fix it?以及如何解决?

In my opinion, that this error is maybe occurring because I incorrectly write method deleteCategory在我看来,这个错误可能是因为我错误地编写了方法deleteCategory

Home.js:主页.js:

const Home = () => {
    
  const [value, setValue] = useState({
     /.....
    listCategory: [],
    numberIdDelete: "",
    isOpenedDelete: false
  });
    
const deleteCategory = (argDeleteCategory) => {    //  in this method id some category set as value in field - numberIdDelete                                   
    setValue({                                     //  that is, I'll know on which category the user has clicked delete button
        ...value,
        numberIdDelete: argDeleteCategory,     //  this id I put in path in component DeleteCategory
        isOpenedDelete: true
    });
};
    
const cancel = () => {                   // close modal window
    setValue({
        ...value,
        isOpenedDelete: false
    });
};
    
  return (
    <div> 
      <Table dataAttribute={value.listCategory} 
             deleteCategory={deleteCategory}/>    // in component Table located button Delete
                 
         {value.isOpenedDelete && <DeleteCategory value={value.numberIdDelete} cancel={cancel} />} 
   </div>
  );
};

DeleteCategory.js:删除类别.js:

const DeleteCategory = (props) => {
    
    const {handleSubmit} = useFormik({  
        onSubmit: async () => {
              const response = await fetch(`pathToServer/${props.value}`, { // put value from state (id some category)
                  method:'DELETE',
                  headers: {/.....}
              });
              const data = await response.json();   
      }});

   return ( 
     <div >
      <form onSubmit={handleSubmit}> 
          <button type="submit" onClick={() => props.delCategory}>Delete</button>
          <button type="submit" onClick={() => props.cancel}>Cancel</button>
      </form>
    </div>
   );
};

Table.js:表.js:

export default (props) => (
  <table>
    <thead>
      <tr>
        <th>ID</th>  
        <th>TITLE</th>
      </tr>
    </thead>
    <tbody>
       {props.dataAttribute.map(item => (
        <tr key={item.id}>
          <td>{item.id} </td>
          <td>{item.title} </td>
          <td><button onClick={() => props.deleteCategory(item.id)}>Delete</button></td>    // Button Delete
        </tr>
      ))}
    </tbody>
  </table>
);

The problem is in the way you are calling useFormik .问题在于您调用useFormik的方式。 The way you are configuring it, there are no input fields associated with it.您配置它的方式,没有与之关联的输入字段。

This causes that when you click the delete button a submit is performed and useFormik tries to build an object with the input values to pass it to onSubmit prop.这导致当您单击删除按钮时执行提交,并且useFormik尝试使用输入值构建一个 object 以将其传递给onSubmit属性。 But there are no inputs.但是没有输入。

Examining your DeleteCategory component code, you don't need to use useFormik .检查您的DeleteCategory组件代码,您不需要使用useFormik You are only showing a confirmation modal after all.毕竟,您只是显示确认模式。 Instead, you could do something like:相反,您可以执行以下操作:

const DeleteCategory = (props) => {
    const delCategory = async () => {
        const response = await fetch(`pathToServer/${props.value}`, { // put value from state (id some category)
            method:'DELETE',
            headers: {/.....}
        });
        const data = await response.json();

        // Do whatever when your request has been performed.
    };

   return ( 
       <div >
          <button type="button" onClick={() => delCategory()}>Delete</button>
          <button type="button" onClick={props.cancel}>Cancel</button>
       </div>
   );
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 错误TypeError:无法将未定义或null转换为对象 - error TypeError: Cannot convert undefined or null to object 无法将未定义或空值转换为对象的错误 - Error with Cannot convert undefined or null to object 反应:错误:无法将未定义或 null 转换为 object - React: error: Cannot convert undefined or null to object 无法将未定义或null转换为对象 - Cannot convert undefined or null to object ReactJS 错误 TypeError:无法将 undefined 或 null 转换为对象 - ReactJS error TypeError: Cannot convert undefined or null to object 渲染错误:“TypeError:无法在 vue js 中将未定义或 null 转换为对象”? - Error in render: “TypeError: Cannot convert undefined or null to object” in vue js? UglifyJs引发错误:无法将未定义或null转换为对象 - UglifyJs throws error: Cannot convert undefined or null to object 在 Vue 组件上出现错误“无法将未定义或 null 转换为对象” - Getting an error “Cannot convert undefined or null to object” on Vue component 数据()中的Vue警告错误“类型错误:无法将未定义或空值转换为对象” - Vue warning Error in data() "TypeError: Cannot convert undefined or null to object" json数据错误未捕获的TypeError:无法将未定义或null转换为对象 - json data error Uncaught TypeError: Cannot convert undefined or null to object
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM