【发布时间】:2015-09-22 10:22:19
【问题描述】:
使用 reactjs:react 作为官方的 react 包尚未正确安装 Windows。
只是试图掌握 React 并且对看似很小的事情感到非常沮丧。由于某种原因,我实际上无法通过我的 React 组件查询我的任何 Mongo 集合 - Chrome 控制台中的基本 Mongo 查询按预期工作......
var ExampleComponent = ReactMeteor.createClass({
getInitialState: function() {
return {data: []};
},
//didn't think the following was necessary, but tried it to no avail:
startMeteorSubscriptions: function() {
Meteor.subscribe('exampleCollection');
},
componentDidMount: function() {
var collection = ExampleCollection.find().fetch();
console.log(collection); //Empty Array []
console.log(ExampleCollection); //Mongo Collection
console.log(ExampleCollection.find()); //Cursor
console.log(ExampleCollection.find().fetch()); //[]?? wtf?
this.setState({data: collection});
},
render: function() {
return (
<div data={this.state.data}>
Hello World?
</div>
);
}
});
Meteor.startup(function() {
React.render(<ExampleComponent />, document.getElementById('root'));
})
那么这里发生了什么?任何帮助将不胜感激,我没有找到我希望的关于使用 React 和 Meteor 做基础知识的资源。
【问题讨论】:
-
您期望发生什么? setState 只会导致重新渲染,但由于 state.data 不会影响显示的内容,我认为显示不会改变?
-
fetch和find都是异步的。因此,当您调用setState时没有结果。
标签: javascript mongodb meteor reactjs