错误:对象不支持属性或方法“getTractorNumber”
我想要做的是如果为空则返回<Link to ={`/assign-tractor/${user_id}`}> Assign Tractor </Link>以呈现,否则返回tractor-id<Link to ={`/tractor-details/${tractorId}`}>{this.tractorNumber}</Link>
如何修复我的代码以使其执行我想要执行的操作?有没有更好的方法来解决这个问题?
注意:我是 React 和 JavaScript 的新手,所以很多语法和语言的细微差别不是很熟悉。请保持答案尽可能基本,以便于消化。
import React, {Link, useState,Component} from 'react'
import axios from 'axios'
import './assignment-table.css';
import {withRouter} from 'react-router-dom';
class AssignmentTable extends Component {
constructor(props){
super(props);
this.state = {
users : [],
isLoggeIn: false,
tractorNumber:"",
orderNumber:""
}
this.getTractorNumber = this.getTractorNumber.bind(this);
}
componentDidMount(){
axios.get(`http://localhost:3000/project/user/drivers`)
.then(response => {
console.log(response.data[0])
this.setState({ users: response.data })
})
.catch( err => console.log(err));
}
async getTractorNumber(tractorId, user_id){
if(tractorId == null || tractorId ==undefined){
return <Link to ={`/assign-tractor/${user_id}`}> Assign Tractor </Link>;
}
else{
axios.get(`http://localhost:3000/project/tractor/${tractorId}`)
.then(response => {
this.setState({ tractorNumber: response.data.tractorNumber })
});
return <Link to ={`/tractor-details/${tractorId}`}>{this.state.tractorNumber}</Link>;
}
}
render(){
let users = this.state.users? this.state.users.map((item, key) => {
return (
<tr>
<td key = {key}>{item.firstName} {item.lastName}</td>
<td>{this.getTractorNumber(item.tractor, item.id)}</td>
</tr>
)
}) : "No Avilable Driver";
return (
<table className="table">
<thead>
<tr>
<th scope="col">Driver Name</th>
<th scope="col">Tractor</th>
<th scope="col">Order</th>
</tr>
</thead>
<tbody>
{users}
</tbody>
</table>
)};
}
export default withRouter (AssignmentTable)