我是新手,我在 udemy 中学习教程,但出现错误:

 Line 45:9:  Parsing error: Unexpected token, expected "," 
 
  43 |     TodoDataService.deleteTodo(username, id).then(() => 
  44 |       this.setState({ message: `Delete of ${id} successfull` }) 
> 45 |         this.refreshTodo() 
     |         ^ 
  46 |        
  47 |     ); 
  48 |   } 
TodoDataService.deleteTodo(username, id) 使用 Web 服务删除列表中的项目,这是整个 TodoDataService 模块的样子:
import Axios from "axios"; 
 
class TodoDataService { 
  retrieveAllTodos(username) { 
    return Axios.get(`http://localhost:8080/users/${username}/todos`); 
  } 
 
  deleteTodo(username, id) { 
    return Axios.delete(`http://localhost:8080/users/${username}/todos/${id}`); 
  } 
 
  updateTodo(username, id) { 
    return Axios.put(`http://localhost:8080/users/${username}/todos/${id}`); 
  } 
} 
 
export default new TodoDataService(); 
我不确定本教程是否太旧并且语法可能从那里发生变化或发生了什么变化。

请您参考如下方法:

您有语法错误。 TodoDataService.deleteTodo(username, id)返回一个 promise ,然后您正在调用 then 并传递一个闭包。但是,您的闭包在语法上无效,因为它有多行代码,但省略了打开和关闭分支。
你应该有:

TodoDataService.deleteTodo(username, id).then(() => { 
  this.setState({ message: `Delete of ${id} successful` }); 
  this.refreshTodo(); 
}); 
如果你有一个单行闭包,它不需要大括号,它会返回值。例如:
const add = (a, b) => a + b; 
> add(1, 2) 
< 3 
它是单行并返回值 1。但是,如果您有多行,则需要使用大括号并显式返回值。所以这在语法上是有效的,但在逻辑上是不正确的,因为它不返回加法的结果。
const brokenAdd = (a, b) => { 
  a + b; 
}; 
> brokenAdd(1, 2) 
< undefined 


评论关闭
IT序号网

微信公众号号:IT虾米 (左侧二维码扫一扫)欢迎添加!