【问题标题】:TypeError: this.state.data.map is not a function when deployed on Heroku but working locallyTypeError: this.state.data.map 在 Heroku 上部署时不是一个函数,但在本地工作
【发布时间】:2018-07-13 03:01:37
【问题描述】:

当我的 react 应用程序部署在 Heroku 上时,我收到 .map is not a function 错误。不过,它在我的本地机器上运行良好。错误发生在我试图映射 this.state.data 的线上。里面 Modal.Content 标记。

我已经检查过 this.state.data 确实是一个数组,并且如前所述,它可以在我的本地主机服务器上运行。我不知道为什么它在部署时不起作用。任何帮助,将不胜感激。谢谢。

link to app on heroku

import React from 'react'
import { Form } from 'semantic-ui-react'
import { Button, Header, Modal, Message, List, Label } from 'semantic-ui-react'
import axios from 'axios'

const styles = {
  formBox: {
    margin: 5
  },
  inputBox: {
    marginTop: 5,
    marginBottom: 5
  }
}

export default class LogHistoryForm extends React.Component {
  state = {
    data: [],
    invalid: false,
    modal: false
  }

  handleChange = (e) => {
    this.setState({
      [e.target.name]: e.target.value
    })
  }

  handleSubmit = (e) => {
    e.preventDefault()
    if(!this.state.username) return this.setState({invalid: true})
    axios.get(`/api/exercise/log?username=${this.state.username}&from=${this.state.from}&to=${this.state.to}`)
      .then(res => {
        if(res.data === 'user not found') return this.setState({invalid: true})
        this.setState({
           data: res.data,
           invalid: false,
           modal: true  
        })
        console.log(res)
      })
      .catch(err => console.log(err))
  }

  closeModal = () => {
    this.setState({ modal: false })
  }

  render() {

    return (
      <Form style={styles.formBox} onSubmit={this.handleSubmit} success error autocomplete='off' >
        <Header as='h3' textAlign='left'>
          Search Exercise History
        </Header>
        <Form.Field>
          <input 
            style={styles.inputBox} 
            type='text'
            name='username'
            placeholder='Username  *Required' 
            onChange={this.handleChange} />
          <Label as='a' color='orange' ribbon>From</Label>
          <input 
            style={styles.inputBox} 
            type='date'
            name='from'
            placeholder='From' 
            onChange={this.handleChange} />
          <Label as='a' color='orange' ribbon>To</Label>
          <input 
            style={styles.inputBox} 
            type='date'
            name='to'
            placeholder='To' 
            onChange={this.handleChange} />
        </Form.Field>
        { this.state.invalid && <Message error header='User not found' content="Please make sure you entered in your username" /> }
        <Button primary data-tooltip='Will display a maximum of 20 results'>Search</Button>
        <Modal open={this.state.modal} onClose={this.closeModal} closeIcon>
          <Header content='History' />
          <Modal.Content>
            {!this.state.data.length && <div>No history found</div>}
            {this.state.data.length && this.state.data.map(item =>
              <List key={item._id}>
                <List.Item key={item._id+1}> {item.date.split('T')[0].split('-').reverse().join('/')} </List.Item>
                <List.Item key={item._id+2}> {item.description} </List.Item>
                <List.Item key={item._id+3}> {item.duration ? item.duration + ' mins' : ''} </List.Item>
                <List.Item key={item._id+4}> {item.reps ? item.reps + ' reps' : ''} </List.Item>
                <List.Item key={item._id+5}> {item.weight ? item.weight + ' kgs' : ''} </List.Item>
              <hr />
              </List>
            )}
          </Modal.Content>
        </Modal>
      </Form> 
    )
  }
}

这是端点的服务器端代码

// end point for getting user's exercise log
app.get('/api/exercise/log', (req, res) =>{
  const username = req.query.username
  const from = Date.parse(req.query.from)
  const to = Date.parse(req.query.to)

  if(!username) return res.json('username required')

  User.findOne({ username }, { log: { $slice: 20 } })
    .exec((err, user) => {
      if (err) return res.json(err)
      if (!user) return res.json('user not found')
      let output;
      if (from && to) {output = user.log.filter(item => Date.parse(item.date) >= from && Date.parse(item.date) <= to)}
      else if (from) {output = user.log.filter(item => Date.parse(item.date) >= from)}
      else if (to) {output = user.log.filter(item => Date.parse(item.date) <= to)}
      else {output = user.log}
      output.sort((obj1, obj2) => obj2.date - obj1.date)
      res.json(output)
    })
})

【问题讨论】:

  • 我从您的应用程序中handleSubmit 的请求中获得 HTML 响应。
  • 对不起,我不确定你的意思是什么,你得到 HTML 作为响应?您能否发布您收到的回复?谢谢。

标签: reactjs heroku


【解决方案1】:
s.a.createElement(l.h.Content, null, !this.state.data.length && s.a.createElement("div", null, "No history found"), this.state.data.length && this.state.data.map(function(e) {})

您在这一行中遇到了错误。我的猜测是 api 返回的是字符串而不是数组。

【讨论】:

    猜你喜欢
    • 2023-01-11
    • 1970-01-01
    • 2022-10-17
    • 2020-08-23
    • 1970-01-01
    • 2020-11-06
    • 1970-01-01
    • 2015-07-24
    • 2020-08-25
    相关资源
    最近更新 更多