Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/469.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 切片工作不正常|反应_Javascript_Reactjs_Splice - Fatal编程技术网

Javascript 切片工作不正常|反应

Javascript 切片工作不正常|反应,javascript,reactjs,splice,Javascript,Reactjs,Splice,在我的构造函数中,我有: constructor(props) { super(props) this.state = { showCount: 4, event: [], customer: [], lock: [] } } componentDidMount() { const { data } = this.props axios.get('http://34.248.65.20:3000/customers').then(res =&

在我的构造函数中,我有:

constructor(props) {
  super(props)
  this.state = {
    showCount: 4,
    event: [],
    customer: [],
    lock: []
  }
}
componentDidMount() {
  const { data } = this.props
  axios.get('http://34.248.65.20:3000/customers').then(res => {
    this.setState({
      res,
      data: res.data
    })
  })

  axios.get('http://34.248.65.20:3000/events').then(res => {
    this.setState({
      res,
      data: res.data
    })
  })

  axios.get('http://34.248.65.20:3000/locks').then(res => {
    this.setState({
      res,
      data: res.data
    })
  })
}
const { event, customer, lock, data } = this.state
const keyedCustomers = _.keyBy(customer, '_id')
const keyedLocks = _.keyBy(lock, '_id')

const events = event
  .slice(0, this.state.showCount)
  .map((event, i) =>
    <EventItem
      key={i}
      event={event}
      customer={keyedCustomers[event.customer] || {}}
      lock={keyedLocks[event.lock] || {}}
    />
  )
console.log(events) // A lot of object
在我的
componentDidMount
中,我有以下内容:

constructor(props) {
  super(props)
  this.state = {
    showCount: 4,
    event: [],
    customer: [],
    lock: []
  }
}
componentDidMount() {
  const { data } = this.props
  axios.get('http://34.248.65.20:3000/customers').then(res => {
    this.setState({
      res,
      data: res.data
    })
  })

  axios.get('http://34.248.65.20:3000/events').then(res => {
    this.setState({
      res,
      data: res.data
    })
  })

  axios.get('http://34.248.65.20:3000/locks').then(res => {
    this.setState({
      res,
      data: res.data
    })
  })
}
const { event, customer, lock, data } = this.state
const keyedCustomers = _.keyBy(customer, '_id')
const keyedLocks = _.keyBy(lock, '_id')

const events = event
  .slice(0, this.state.showCount)
  .map((event, i) =>
    <EventItem
      key={i}
      event={event}
      customer={keyedCustomers[event.customer] || {}}
      lock={keyedLocks[event.lock] || {}}
    />
  )
console.log(events) // A lot of object
在我的渲染中,我有这样一个:

constructor(props) {
  super(props)
  this.state = {
    showCount: 4,
    event: [],
    customer: [],
    lock: []
  }
}
componentDidMount() {
  const { data } = this.props
  axios.get('http://34.248.65.20:3000/customers').then(res => {
    this.setState({
      res,
      data: res.data
    })
  })

  axios.get('http://34.248.65.20:3000/events').then(res => {
    this.setState({
      res,
      data: res.data
    })
  })

  axios.get('http://34.248.65.20:3000/locks').then(res => {
    this.setState({
      res,
      data: res.data
    })
  })
}
const { event, customer, lock, data } = this.state
const keyedCustomers = _.keyBy(customer, '_id')
const keyedLocks = _.keyBy(lock, '_id')

const events = event
  .slice(0, this.state.showCount)
  .map((event, i) =>
    <EventItem
      key={i}
      event={event}
      customer={keyedCustomers[event.customer] || {}}
      lock={keyedLocks[event.lock] || {}}
    />
  )
console.log(events) // A lot of object
const{event,customer,lock,data}=this.state
const keyedCustomers=u.keyBy(客户,_id'))
const keyedLocks=u.keyBy(锁、_id'))
常量事件=事件
.slice(0,此.state.showCount)
.map((事件,i)=>
)
console.log(events)//很多对象
下面是对象
事件

但是我没有在我的应用程序中看到我的任何事件(一个事件基本上是一个
li
)。我认为这将默认显示我的四个事件(
showCount:4
)。因为我想有一个按钮,稍后将显示所有这些

有人能告诉我我做错了什么吗


干杯

如果您的
res
{event:[]}
不同,您应该执行以下操作:

 axios.get('http://34.248.65.20:3000/events').then(res => {
    this.setState({
      ...
      event: res.data
    })
  })

如果您的
res
{event:[]}
不同,您应该执行以下操作:

 axios.get('http://34.248.65.20:3000/events').then(res => {
    this.setState({
      ...
      event: res.data
    })
  })

请添加更多代码来说明此问题。渲染函数中的事件是什么?你的意思是
this.state.event
?你在控制台中看到错误了吗?@DanielAndrei已经添加了更多的代码。@MaximKuzmin Nopp,没有错误:/@MartinNordström你需要发布“event”对象的示例(你可以通过调试器或console.log检查),请添加更多代码来说明这个问题。渲染函数中的事件是什么?你的意思是
this.state.event
?你在控制台中看到错误了吗?@DanielAndrei已经添加了更多的代码。@MaximKuzmin Nopp,没有错误:/@MartinNordström你需要发布“event”对象的示例(你可以通过调试器或console.log检查它)