我使用react来构建过滤搜索我的数据。这一切都正常,但后来我得到了一个错误:
未捕获(承诺)类型错误:无法读取属性' toLowerCase'未定义的。
当我将状态搜索更新为null时,错误变为
' toLowerCase'为null。
但是当我把它作为一个空字符串时,错误变为undefined
。
就像我说的,一切都运转良好,这个错误突然来了,请我需要你的帮助
继承代码
var React = require('react');
var firebase = require('firebase');
var $ = require('jquery');
var axios = require('axios');
var notify = require('bootstrap-notify');
var {Link} = require('react-router');
var SuppliersList = React.createClass({
getInitialState: function(event){
return{
search:'Nicolas',
data: [],
}
},
updateSearch : function(event) {
//this.setState({search: event.target.value.substr(0, 20)});
},
componentDidMount: function(){
firebase.database().ref('/suppliers/').once('value').then(function(snapshot) {
var arr = Object.values(snapshot.val());
this.setState({data: arr,activity:false});
}.bind(this));
},
render: function () {
var filteredSuppliers = this.state.data.filter(
(item) => {
return item.name.toLowerCase().indexOf(
this.state.search.toLowerCase()) !== -1;
}
)
var items = filteredSuppliers.map((item, i)=>
<tr key={item.id}>
<td>{item.name}</td>
<td>{item.email}</td>
<td>{item.phone}</td>
<td><Link className="btn btn-primary" to={'/edit/'+item.id}>Edit</Link></td>
<td><Link className="btn btn-danger" to={'/delete/'+item.id}>Delete</Link></td>
<td><a href="#" className="btn btn-warning" role="button" data-title="johnny" data-id="1">Favourite</a></td>
</tr>
);
return (
<div>
<div style={{marginLeft:'50%'}} className="loader"></div>
<input type="text" value={this.state.search} style={{marginTop:20}} placeholder="search a supplier" onChange={this.updateSearch} className="form-control" id="usr"/>
<table className="table table-striped table-hover table-users">
<thead>
<tr>
<th>Name</th>
<th>Email</th>
<th>Phone</th>
<th></th>
<th></th>
</tr>
</thead>
<tbody>
{items}
</tbody>
</table>
</div>
)},
});
var Suppliers = React.createClass({
render: function(){
return (
<div>
<nav className="navbar navbar-default navbar-fixed">
<div className="container-fluid">
<div className="navbar-header">
<button type="button" className="navbar-toggle" data-toggle="collapse" data-target="#navigation-example-2">
<a href="#/branch">
<i className="fa fa-store"></i>
</a>
</button>
<button type="button" className="navbar-toggle" data-toggle="collapse" data-target="#navigation-example-2">
<a href="#/delete">
<i className="fa fa-trash"></i>
</a>
</button>
<button type="button" className="navbar-toggle" data-toggle="collapse" data-target="#navigation-example-2">
<a href="#/edit">
<i className="fa fa-edit"></i>
</a>
</button>
<button type="button" className="navbar-toggle" data-toggle="collapse" data-target="#navigation-example-2">
<a href="#/add">
<i className="fa fa-plus"></i>
</a>
</button>
<a className="navbar-brand" href="#">Suppliers</a>
</div>
<div className="collapse navbar-collapse">
<ul className="nav navbar-nav navbar-left">
</ul>
<ul className="nav navbar-nav navbar-right">
<li>
<a href="#/add">
<i className="fa fa-plus"></i>
</a>
</li>
<li>
<a href="#/delete">
<i className="fa fa-trash"></i>
</a>
</li>
<li>
<a href="#/edit">
<i className="fa fa-edit"></i>
</a>
</li>
<li onClick={this.refresh}>
<a>
<i className="fa fa-refresh"></i>
</a>
</li>
<li>
<a href="#/branch">
<i className="fa fa-map-marker"></i>
</a>
</li>
</ul>
</div>
</div>
</nav>
<table className="table table-hover table-striped">
<SuppliersList/>
</table>
</div>
)},
refresh: function(){
location.reload();
}
});
module.exports = Suppliers;
答案 0 :(得分:2)
问题在于这段代码
var filteredSuppliers = this.state.data.filter(
(item) => {
return item.name.toLowerCase().indexOf(
this.state.search.toLowerCase()) !== -1;
}
)
this.state.search未定义/ null,因此您无法读取它的属性。
我建议只返回所有项目以防搜索未定义/ null,通常是任何falsey值,因此将该代码更改为:
var filteredSuppliers = this.state.search ? this.state.data.filter(
(item) => {
return item.name.toLowerCase().indexOf(
this.state.search.toLowerCase()) !== -1;
}
) : this.state.data
如果你收到错误
'undefined' is not a function (evaluating 'item.name.toLowerCase()')
然后你需要调用item.name.toString.toLowerCase()...
,因为你只能在字符串上运行toLowerCase。