使用React构建的搜索栏查询Firebase



相对较新的反应和尝试将搜索栏纳入我的firebase数据库中的'企业节点。可以在我的组件流中使用一些指导/输入。

我的最初本能告诉我,应该使用"路由器> searchbarlogic> searchbar> businessIndexlogic> businessIndex"之类的东西,其中一个术语从我的搜索栏传递到我的businessIndexlogic,以帮助产生我的查询。

从下面提到的组件中粘贴相关代码。任何见解都将不胜感激。

router.js

const router = (
  <Router history={browserHistory} >
    <Route path='/' component={App} >
       <IndexRoute component={HomeLogic} />
       <Route path='businesses' >
         <IndexRoute component={SearchBarLogic} />
         <Route path='addbusiness' component={AddBusinessFormLogic} />
         <Route path=':id' >
           <IndexRoute component={BusinessesProfileLogic} />
           <Route path='deletebusiness' component={DeleteBusinessPageLogic} />
         </Route>
       </Route>   
   </Router>
);

searchbarlogic.js

import React, {Component} from 'react';
import SearchBar from '../layouts/SearchBar';
class SearchBarLogic extends Component {
  constructor(props){
    super(props);
    this.state = {
      term: "",
    };
  }
  search(term) {
    this.setState({ term });
  }
  render(){
    let term = this.state.term
    return (
      <div>
        <SearchBar
          term={term}
          onSearchTermChange={(term) => {this.search(term)}}
        />
      </div>
    )
  }
}
export default SearchBarLogic;

searchbar.js

import React, {Component} from 'react';
import BusinessIndexLogic from '../logic/businesses/BusinessIndexLogic';
class SearchBar extends Component {
  render(){
  return (
    <div>
      <div className="nav-wrapper">
        <form>
          <div className="input-field">
            <input
              id="search"
              type="search"
              value={this.props.term}
              onChange={(event) => {this.props.onSearchTermChange(event.target.value)}}
            />
          </div>
        </form>
      </div>
      <div>
        <BusinessIndexLogic
          value={this.props.term}
        />
      </div>
    </div>
  );
  }
}
export default SearchBar;

businessIndexlogic.js

import React, { Component } from 'react';
import { BusinessStorage } from '../../../config/FirebaseConstants';
import BusinessIndex from '../../pages/businesses/BusinessIndex';
class BusinessIndexLogic extends Component {
  constructor(props){
    super(props);
    this.state = {
      allBusinesses: [],
    }
  }
  componentDidMount() {
    let _this = this;
    let businesses = {};
    if (this.props.value === ""){
      BusinessStorage.ref('users/').once('value', function(allData) {
        allData.forEach(function(data) {
          data.forEach(function(userNode) {
            if (userNode.val().type === 'businesses') {
              if (userNode.val().deletedAt !== true) {
                let companyName = userNode.val().companyName;
                let idNumber = data.key;
                businesses[companyName] = {name: companyName, id: idNumber}
              }
            }
          })
        })
      }).then(function(user) {
        let keys = Object.keys(businesses).sort();
        let sortedHash = {};
        let sortedArray = [];
        for (let i = 0; i < keys.length; i++) {
          let key = keys[i];
          sortedArray.push(sortedHash[key] = businesses[key]);
        }
        _this.setState({allBusinesses: sortedArray}, function afterBusinessSet() {
        });
      })
  } else {
    BusinessStorage.ref('users/').startAt(`${this.props.value}`).once('value', function(allData) {
      allData.forEach(function(data) {
        data.forEach(function(userNode) {
          if (userNode.val().type === 'business') {
            if (userNode.val().deletedAt !== true) {
              let companyName = userNode.val().companyName;
              let idNumber = data.key;
              businesses[companyName] = {name: companyName, id: idNumber}
            }
          }
        })
      })
    }).then(function(user) {
      let keys = Object.keys(businesses).sort();
      let sortedHash = {};
      let sortedArray = [];
      for (let i = 0; i < keys.length; i++) {
        let key = keys[i];
        sortedArray.push(sortedHash[key] = businesses[key]);
  }
      _this.setState({allBusinesses: sortedArray}, function afterBusinessSet() {
      });
    })
  }
  }
  render() {
    let allBusinesses = this.state.allBusinesses;
    let pathName = this.props.location.pathname;
    return (
      <BusinessIndex
        allBusinesses={allBusinesses}
        pathName={pathName}
      />
    )
  }
}
export default BusinessIndexLogic;

,我目前在控制台中遇到的错误是Uncaught TypeError: Cannot read property '_currentElement' of null,这无济于事。就像我说的那样,任何洞察力或建议都将不胜感激。谢谢。

据此,该错误意味着您的代码正在抛出另一个错误,该错误阻止了您的组件渲染。控制台中有一个错误吗?如果没有,请添加Console.Logs的错误处理。这可能是您的承诺链中的东西(firebase Call);将.catch(err => console.log(err))添加到链的末端,但通常会在整个过程中进行一些错误处理。

最新更新