未处理的拒绝(TypeError):res.map不是函数

时间:2018-07-06 16:10:19

标签: javascript reactjs react-router

搜索“ andkk”时出现此错误:

  

未处理的拒绝(TypeError):res.map不是函数

这是我的代码:

state = {
  ListBook: [],
  SaveQuery: '',
}

SearchBook = (query) => {
  if (query.trim() !== '') {
    BooksAPI.search(query).then((res) => {
      res.map(book => (this.props.allBooks.filter((b) => b.id === book.id).map(b => book.shelf = b.shelf)))
      if (res && res.length) this.setState({
        ListBook: res,
        SaveQuery: query
      })
      if (res.error) this.setState({
        ListBook: [],
        SaveQuery: 'query'
      })
    })
  } else {
    this.setState({
      ListBook: []
    })
  }
}

1 个答案:

答案 0 :(得分:0)

我找到了解决方法:

BooksAPI.search(query).then((res) => {
          if (res && res.length) this.setState({ListBook: res, SaveQuery: query})
          if (res.error) return res
res.map(book => (this.props.allBooks.filter((b) => b.id === book.id).map(b => book.shelf = b.shelf)))