使用map函数之外的变量进行反应

时间:2017-02-20 23:28:09

标签: javascript reactjs dictionary

我对某些变种有点问题。

我的代码是:

import React from 'react';
import mongoose from 'mongoose';
var sch = require('../models/schema');

export default class IndexPage extends React.Component {
  render() {

    sch.find(function(err,models){
       //???
    });

    return (
      <div className="home">
        Afiez ceva {models}
      </div>
    );

  }
}

如何实现这一目标?我尝试了所有的posibilites。 我希望能够呈现像{models}或{models [0] .whatever}这样的东西。 请问一些提示?

参考sch:

import mongoose from 'mongoose';

var schValue = new mongoose.Schema({
  asd: String
});

schValue.set( 'collection', 'ecommerce' );

module.exports = mongoose.model('sch', schValue);

1 个答案:

答案 0 :(得分:1)

我没有测试过,但是你需要这样的东西:

  export default class IndexPage extends React.Component {

  constructor() {
    super();

    this.state = {
      models: [],
    };
  }

  componentWillMount() {
    sch.find({}, (err, models) => {
      this.setState({ models });
    });
  }

  render() {
    return (
      <div className="home">
        {this.state.models.map(model => {
          return model;
        })}
      </div>
    );
  }
}