繁体   English   中英

无法读取未定义的属性'toLowerCase' - REACT - FIRESTORE

[英]Cannot read property 'toLowerCase' of undefined - REACT - FIRESTORE

我对React和Firestore有点新意,并且已经试图弄清楚发生了几个小时的情况。 我尝试使我的过滤器功能使用从APP.js中的Firestore收到的数据。 我将数据{tasks,searchTerm}传递给DASHBOARD组件。 过滤器在使用状态和道具之前工作,但在使用firestore数据替换状态中的硬编码数据后,它不再起作用,并且在DASHBOARD组件中过滤数组时出现以下错误: 无法读取属性'toLowerCase '未定义

我试图直接向TASKS.js发送没有任何过滤的数据,这是正常工作(所有任务都显示)。 但是一旦我通过newArray,它就不再起作用了。

此外,当在DASHBOARD组件中的tasks.filter函数中记录task.title时,它会显示所有数据(由于数据来自Firestore,所以会有一点延迟)

APP.JS -

import React, { Component } from 'react';
import './App.css';
import Dashboard from './Components/Dashboard/Dashboard'
import AddTask from './Components/Tasks/Task/AddTask'
import Navbar from './Components/Navbar/Navbar'
import Searchbar from './Components/Searchbar/Searchbar'
import firebase from './Firebase';

class App extends Component {
  constructor(props) {
    super(props)
    this.ref = firebase.firestore().collection('tasks')
    this.state = {
      tasks: [],
      searchTerm: ""
    }

    this.handleLikeButton = this.handleLikeButton.bind(this)
    this.handleRemoveButton = this.handleRemoveButton.bind(this)
    this.addTask = this.addTask.bind(this)
    this.handleFilter = this.handleFilter.bind(this)
  }

  componentWillMount() {
    const db = firebase.firestore()
    const allTasks = []
    db.collection('tasks').onSnapshot(collection => {
       const tasks = collection .docs.map(doc => doc.data())
       this.setState({ tasks: tasks, searchTerm: "" })
    })
  }

  handleLikeButton = (task) => (e) => {
    const tasks = [...this.state.tasks]
    const index = tasks.indexOf(task)
    tasks[index].likes++
    this.setState({
      tasks: tasks
    })
  }

  addTask = (taskName) => (e) => {
    this.ref.add({
      id: Math.floor(Math.random() * 100000000000000),
      title: taskName,
      likes: 0
    })
  }

  handleRemoveButton = (removingTask) => (e) => {
    const tasks = [...this.state.tasks]
    const newTasks = tasks.filter(task => removingTask.id !== task.id)
    this.setState({
      tasks: newTasks
    })
  }


  handleFilter = (searchTerm) => {
    this.setState({
      searchTerm: searchTerm
    })
  }

  render() {
    return (
      <div className="App">
        <Navbar />
        <Searchbar handleFilter={this.handleFilter} />
        <AddTask addTask={this.addTask} />
        <Dashboard tasks={this.state.tasks} searchTerm={this.state.searchTerm} handleLikeButton={this.handleLikeButton} handleRemoveButton={this.handleRemoveButton}/>
      </div>
    );
  }
}

export default App;

DASHBOARD.JS -

import React, { Component } from 'react'
import Tasks from '../Tasks/Tasks'

class Dashboard extends Component {
  constructor(props) {
    super(props)

    this.filterTasks = this.filterTasks.bind(this)
  }

  filterTasks = () => {
      const tasks = [...this.props.tasks]
      const newArray = tasks.filter(task =>
        task.title.toLowerCase().indexOf(this.props.searchTerm.toLowerCase()) > -1)
      return (
        <Tasks tasks={newArray} handleLikeButton={this.props.handleLikeButton} handleRemoveButton={this.props.handleRemoveButton}  />
      )
  }

  render() {
    return (
      <div>
        <h2>Dashboard</h2>
        {this.filterTasks()}
      </div>
    )
  }
}


export default Dashboard

ADDTASK.JS

import React, { Component } from 'react'

class AddTask extends Component {

  constructor(props) {
    super(props)

    this.state = {
      addNewTaskFieldEmpty: true,
      taskName: ""
    }

    this.onChangeHandler = this.onChangeHandler.bind(this)
    this.disableButton = this.disableButton.bind(this)
  }


  onChangeHandler(e) {
    this.setState({
      taskName: e.target.value,
    })
    this.disableButton(e.target.value)
  }

  disableButton(taskName) {
    if(taskName.length == 0) {
      this.setState({addNewTaskFieldEmpty: true})
    } else {
      this.setState({addNewTaskFieldEmpty: false})
    }
  }


  render() {
    return (
      <div>
        <div className="mdc-text-field half-size">
          <input className="mdc-text-field__input " onChange={this.onChangeHandler}  />
          <div className="mdc-line-ripple"></div>
          <label className="mdc-floating-label">Task Name</label>
        </div>
        <a className={"btn-floating btn-large waves-effect waves-light red " + (this.state.addNewTaskFieldEmpty ? 'disabled' : '')} onClick={this.props.addTask(this.state.taskName)}><i className="material-icons">add</i></a>
      </div>
    )
  }
}

export default AddTask

App.css任何错误。

我遇到了这条消息。 我追溯到CSS包括:

.box-table { border-color:; border: 1px solid #dbdad8; }

border-color:的缺失值border-color:导致npm run build失败。

有趣的是,包含相同的文件

.submenu-button.submenu-opened:after { background:; }

这根本没有引起任何问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM