Javascript Vimeo频道和未播放的视频

Javascript Vimeo频道和未播放的视频,javascript,reactjs,api,get,axios,Javascript,Reactjs,Api,Get,Axios,我正在使用Vimeo API使用axios和react访问通道。我已正确设置代码,但编译后出现以下错误: TypeError:无法读取未定义的属性“map” 关于这一行:{this.state.videos.map(video=> 以下是完整的代码供参考: import React, { Component } from 'react'; import axios from 'axios'; class Apicall extends Component { componentWillM

我正在使用Vimeo API使用
axios
react
访问通道。我已正确设置代码,但编译后出现以下错误:

TypeError:无法读取未定义的属性“map”

关于这一行:
{this.state.videos.map(video=>

以下是完整的代码供参考:

import React, { Component } from 'react';
import axios from 'axios';

class Apicall extends Component {

  componentWillMount() {
    this.getChannel();
  }


  getChannel() {
    axios.get(`https://api.vimeo.com/channels/collegehumorbackstage/page:1`)
      .then(res => {
        const videos = res.data.data.children.map(obj => obj.data);
        this.setState({videos});
      });
  }

  constructor(props) {
    super(props);

    this.state = {
      channel_id: 'collegehumorbackstage',
      data: [],
      per_page: '5',
      paging: {
        first: '/channels/collegehumorbackstage/videos?page=1',
        last: '/channels/collegehumorbackstage/videos?page=2'
      }
    }
    this.getChannel = this.getChannel.bind(this);

  }


  render() {
    return (
      <div className="container">
        <ul>
          {this.state.videos.map(video =>
            <li key={video.uri}></li>
          )}
        </ul>
      </div>
    );
  }
}

export default Apicall;
import React,{Component}来自'React';
从“axios”导入axios;
类Apicall扩展组件{
组件willmount(){
这个.getChannel();
}
getChannel(){
axios.get(`https://api.vimeo.com/channels/collegehumorbackstage/page:1`)
。然后(res=>{
const videos=res.data.data.children.map(obj=>obj.data);
this.setState({videos});
});
}
建造师(道具){
超级(道具);
此.state={
频道id:“大学后台”,
数据:[],
每页:“5”,
寻呼:{
第一个:'/channels/collegehumorbackstage/videos?page=1',
最后:'/channels/collegehumorbackstage/videos?第2页'
}
}
this.getChannel=this.getChannel.bind(this);
}
render(){
返回(
    {this.state.videos.map(video=>
  • )}
); } } 全部导出默认值;

据我所知,其他一切都很好,还有第二双眼睛总是有帮助的。我是不是遗漏了什么?

这是因为最初你所在的州没有
视频。所以它是
未定义的

您应该为其提供默认值,例如空数组:

this.state = {
   channel_id: 'collegehumorbackstage',
   data: [],
   per_page: '5',
   paging: {
      first: '/channels/collegehumorbackstage/videos?page=1',
      last: '/channels/collegehumorbackstage/videos?page=2' 
   },
   videos: [],
}

真不敢相信我错过了那部分谢谢让我试试that@MikeL5799如果我的回答对你有帮助-请不要忘记接受是正确的。谢谢我为授权令牌添加了一些参数,但它仍然不起作用