简体   繁体   English

在React中无法从mongodb获取数据

[英]can't get data from mongodb in React

i have an app which loads data from mongodb to list component like this 我有一个应用程序可以从mongodb加载数据到这样的列表组件

React.render(<ProductsList url="http://localhost:3000/data"/>, document.getElementById('products'));

and then i try make a http get call to ge the data like this 然后我尝试拨打http get呼叫ge这样的数据

var ProductsList = React.createClass({
  loadData: function() {
    $.ajax({
     url: this.props.url,
     dataType: 'json',
     success: function(data) {
       <ProductsList url={data}/>
     },
     error: function(xhr, status, err) {
       console.error(this.props.url, status, err.toString());
     }
   });
 },
    render: function() {
      this.loadData();
        var products = this.props.url.map(function(product) {
            return (
              <li key={product._id}>
                <Product data={product} />
              </li>
            )
        });

        return (
          <ul className="clearfix">
            {products}
          </ul>
        );
    }
});

but when i load the app i get this error Uncaught TypeError: this.props.url.map is not a function what could the problem be ? 但是,当我加载该应用程序时,出现此错误Uncaught TypeError:this.props.url.map不是一个函数 ,可能是什么问题?

map is not a function for strings, check the documentation for array map: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map map不是字符串函数,请查看数组映射的文档: https : //developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Array/map

Furthermore, not related to your error, but your function loadData returns undefined, because you are just making the ajax call. 此外,与错误无关,但是函数loadData返回未定义,因为您只是在进行ajax调用。 In order to make this work, you need to use the state object of the react component call this.setState() in your success/error hooks. 为了使它起作用,您需要在成功/错误挂钩中使用react组件的state对象调用this.setState()

it's seems like you are not clear about the props and state of React you have to follow in below mentioned way 似乎您不清楚React的道具和状态,您必须按照以下提到的方式进行操作

var ProductsList =React.createClass({
  getInitialState:function()
  {
    return{
      url:[]
    }
  },
  componentDidMount:function()
  {
    var that=this;
     $.ajax({
     url: 'https://api.github.com/users/mralexgray/repos',
     dataType: 'json',
     success: function(data) {
      that.setState({url:data});
     },
     error: function(xhr, status, err) {
       console.error(this.props.url, status, err.toString());
     }
   });
  },
  ChangeContent:function()
  {
    this.setState({content:"change Content"});
  },
  render:function()
  {
     var Products = this.state.url.map(function(product) {
            return (
              <li key={product._id}>
                {product.full_name}
              </li>
            )
        });
    return(
      <div>
       {Products}
      </div>
      )
  }
});
React.render(
  <ProductsList  />,
  document.getElementById('example')
);

Demo 演示版

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

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