Javascript 无法读取属性'_id';未定义的未处理拒绝

Javascript 无法读取属性'_id';未定义的未处理拒绝,javascript,reactjs,Javascript,Reactjs,出于某种原因,我正在从类组件转移到功能组件,最后一个组件正在破坏应用程序 之前有一个组件没有安装;这就是我尝试使用useEffect refreshContent的原因,但如果将其删除,应用程序的某些功能将停止工作 class UserSummary extends Component { constructor(props) { super(props); this.state = initialState }; async componentDidMount() { this.ref

出于某种原因,我正在从类组件转移到功能组件,最后一个组件正在破坏应用程序

之前有一个组件没有安装;这就是我尝试使用useEffect refreshContent的原因,但如果将其删除,应用程序的某些功能将停止工作

class UserSummary extends Component {

constructor(props) {
super(props);
this.state = initialState
  };

async componentDidMount() {
this.refreshContent();
}
代码示例:

import React, { useState, useEffect } from 'react';
import { Link } from 'react-router-dom';
import './UserSummary.css';
import todoService from '../../utils/todoService';
import EditTodoButton from '../EditTodoButton/EditTodoButton';

function UserSummary(props) {
  const [state, setState] = useState ({
    todo: {
      done: false
    }
  });

  useEffect(() => {
    refreshContent();
  });

  async function refreshContent() {
    const todos = await todoService.index(props.user);
    console.log( "FIRST ", todos)
    props.handleUpdateTodos(todos);
  }

  async function handleDeleteToDo(todo){
    await todoService.deleteToDo(todo);
    refreshContent();
  }

  async function handleEditToDo(todo, updatedToDo){
    await todoService.editToDo(todo, updatedToDo);
    refreshContent();
  }

  async function handleUpdateToDo(todo) {
    let update = todo;
    if (update.done) {
      update.done = false
    } else {
      update.done = true
    }
    await todoService.doneToDo(todo);
    const todos = await todoService.index(props.user);
    props.handleUpdateTodos(todos);
  }

  function TodoRows() {
    const { todos } = props;
    const todoRows = todos.map((todo, index) => (
      <ul key={index}>
        <li className="ToDoList">
          <input type="checkbox" defaultValue={setState.done} name="done" checked={todo.done} onChange={() => handleUpdateToDo(todo)} />&nbsp;&nbsp;Done&nbsp;&nbsp;
          <button onClick={() => handleDeleteToDo(todo)}><span role="img" aria-label="delete">
createToken
can return an error, but it doesn't reject the promise, instead the returned token will be null. You have to handle the error like this :


this.props.stripe.createToken({name : 'Name').then(({token, error}) => {
  if (error) {
    // handle error
  } else {
    // handle token
  }
});

import React,{useState,useffect}来自“React”;
从'react router dom'导入{Link};
导入“/UserSummary.css”;
从“../../utils/todoService”导入todoService;
从“../EditTodoButton/EditTodoButton”导入EditTodoButton;
功能用户摘要(props){
常量[状态,设置状态]=使用状态({
待办事项:{
完成:错误
}
});
useffect(()=>{
刷新内容();
});
异步函数refreshContent(){
const todos=wait todoService.index(props.user);
console.log(“第一”,待办事项)
道具。手持更新todos(todos);
}
异步函数handleDeleteToDo(todo){
等待todoService.deleteToDo(todo);
刷新内容();
}
异步函数handleEditToDo(todo,updatedodo){
等待todoService.editToDo(todo,updatedodo);
刷新内容();
}
异步函数handleUpdateToDo(todo){
让update=todo;
如果(update.done){
update.done=false
}否则{
update.done=true
}
等待todoService.doneToDo(todo);
const todos=wait todoService.index(props.user);
道具。手持更新todos(todos);
}
函数TodoRows(){
const{todos}=props;
const todoRows=todos.map((todo,index)=>(
  • handleUpdateToDo(todo)}/>完成
    handleDeleteToDo(todo)}>
    createToken
    可以返回错误,但它不会拒绝承诺,而是返回的令牌将为null。您必须像这样处理错误:


    如果您这样做,您应该能够找到潜在的错误。

    这可能会或可能不会正确地解决您的问题,因为我对这一点的理解很松散,但我尽了最大努力,只掌握了一点知识。您是否正确地传递了用户道具?能否请您显示安装
    UserSummary
    co的代码组件?
    从“React”导入React;从“../../components/UserSummary/UserSummary”导入UserSummary;函数UserSummaryPage(props){return();}导出默认UserSummaryPage;
    @MaddyBlacklisted