[英]Parsing error: Unexpected token, expected "," with react.js and promise
我是 React 新手,我正在學習 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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.