我对某些变种有点问题。
我的代码是:
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);
答案 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>
);
}
}