【问题标题】:Meteor 1.3, React, React Router - data not getting passed throughMeteor 1.3,React,React Router - 数据未通过
【发布时间】:2016-05-24 17:34:06
【问题描述】:

我正在尝试使用 Meteor 1.3、React 和 React-Router 开发一个非常基本的应用程序。页面正在呈现,但在获取数据时遇到问题。我已经进行了一些研究,但无法找到关于这种特殊组合的很多信息,包括容器模式的使用。

应用所需要做的就是在“测试”页面上显示“Thingies”集合中的所有项目。不确定是数据发布、路由、容器还是其他什么不正确?

调试控制台行在集合中都显示 0,即使 mongo shell 肯定会在其中显示项目。

我的项目结构:http://i.stack.imgur.com/WZXFa.png

things.js

import { Meteor } from 'meteor/meteor';
import { Mongo } from 'meteor/mongo';
import { check } from 'meteor/check';

export const Thingies = new Mongo.Collection('thingies');

if (Meteor.isServer) {
  // This code only runs on the server
  Meteor.publish('thingies', function thingiesPublication() {
    return Thingies.find();
  });
}

Test.jsx

import { Meteor } from 'meteor/meteor';
import { Thingies } from '../../api/things.js';
import { createContainer } from 'meteor/react-meteor-data';
import React, { Component } from 'react';

class TestContainer extends Component {
  render(){

    console.log('Props: ' + this.props.thingies.length);

    let RenderThings = this.props.thingies.map(thing => {
      return <li key={thing._id}>{thing.text}</li>
    });

    return (
      <div>
        <h1>Test this</h1>
        <ul>
          { RenderThings }
        </ul>
      </div>
    );
  }
}

TestContainer.propType = {
  thingies: React.PropTypes.array
};

export default createContainer(() => {
  Meteor.subscribe('thingies');

  console.log('Container: ' + Thingies.find({}).count());

  return {
    thingies: Thingies.find({}).fetch(),
  };
}, TestContainer);

routes.jsx

import React from 'react';
import { Router, Route, IndexRoute, browserHistory } from 'react-router';

// route components
import App from '../../ui/layouts/App.jsx';
import TestContainer from '../../ui/pages/Test.jsx';
import Index from '../../ui/pages/Index.jsx';

export const renderRoutes = () => (
  <Router history={browserHistory}>
    <Route path="/" component={ App }>
      <IndexRoute component={ Index } />
      <Route path="index" component={Index}/>
      <Route path="test" component={TestContainer}/>
    </Route>
  </Router>
);

Navigation.jsx

import React from 'react';
import { IndexLink, Link } from 'react-router';

export const Navigation = () => (
<div>
<h4>Navigation</h4>
  <ul>
    <li><IndexLink to="/" activeClassName="active">Home</IndexLink></li>
    <li><Link to="index" activeClassName="active">Index</Link></li>
    <li><Link to="test" activeClassName="active">Test</Link></li>
  </ul>
  </div>
)

App.jsx

import React, { Component }  from 'react';
import { Navigation } from '../components/Navigation.jsx';

const App = ( { children } ) => (
  <div>
    <Navigation />
    { children }
  </div>
)

export default App;

提前非常感谢。我确定我遗漏了一些明显的东西!

【问题讨论】:

    标签: javascript meteor reactjs


    【解决方案1】:

    我会尝试稍微更改一下 Test.jsx Container 代码:

    export default createContainer(() => {
    
      if (Meteor.subscribe('thingies').ready()) {
    
        console.log('Container: ' + Thingies.find({}).count());
    
        return {
          thingies: Thingies.find({}).fetch()
        };
      } else {
        return {
          thingies: null
        };
      }
    }, TestContainer);
    

    编辑:尝试将您的发布方法替换为:

    if (Meteor.isServer) {
      // This code only runs on the server
      Meteor.publish('thingies', function() {
        return Thingies.find({});
      });
    }
    

    【讨论】:

    • 谢谢,试过了,发现订阅还没有准备好,但它似乎永远不会准备好??
    • 用一些额外的信息编辑了我的答案:
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-08-09
    • 1970-01-01
    • 2015-03-20
    • 2021-04-20
    • 1970-01-01
    • 2017-06-17
    • 2016-07-17
    相关资源
    最近更新 更多