【问题标题】:Data not being exposed to react component from meteor publish数据未暴露于来自流星发布的反应组件
【发布时间】:2016-04-22 01:18:07
【问题描述】:

也许我在这里遗漏了一些东西,但我没有让数据从我的数据库流入我的组件。

我正在使用 react 和 meteor (w/ mantra),在服务器上使用 simpleSchema/collection2。据我所知,我的发布/订阅设置正确。

我已经用 mantra cli 构建了一些东西。 (顺便说一句很棒) 这是我的设置:

组件:(client/modules/components/sidebar_hubs.js)

import React from 'react';
const renderIfData = ( hubs ) => {
if ( hubs && hubs.length > 0 ) {
     return hubs.map( ( hub ) => {
      return <li key={ hub._id }>{ hub.name }</li>;
    });
} else {
return <p>No hubs yet!</p>;
}
};

const SidebarHubs = ({hubs}) => (

 <div>
     <h1> Hubs </h1>
     {hubs.length}
     <ul> { renderIfData( hubs ) } </ul>
  </div>
);

export default SidebarHubs;

容器(client/modules/containers/sidebar_hubs.js)

import {useDeps, composeAll, composeWithTracker, compose} from 'mantra-core';
import SidebarHubs from '../components/sidebar_hubs.js';


export const composer = ({context}, onData) => {

 const {Meteor, Collections} = context();
 const subscription = Meteor.subscribe( 'hubs' );

 if ( subscription.ready() ) {
   const hubs = Collections.Hubs.find().fetch();
   onData( null, { hubs } );
 }

};

 export const depsMapper = (context, actions) => ({
   context: () => context
 });

export default composeAll(
  composeWithTracker(composer),
  useDeps(depsMapper)
)(SidebarHubs);

发布:(server/publications/hubs.js)

 import {Hubs} from '/lib/collections';
 import {Meteor} from 'meteor/meteor';
 import {check} from 'meteor/check';

 export default function () {
   Meteor.publish('hubs', function () {
     return Hubs.find();
   });
 }

我在这里明显遗漏了什么?

【问题讨论】:

  • 你能详细说明结果吗? (组件渲染了吗?它渲染时没有数据吗?你检查 DDP 订阅以确定数据是否被推送到客户端?你在 MiniMongo 本身中有数据吗?)
  • 刚刚发布了答案,它没有意识到我是直接导入我的组件而不是我的组件容器。

标签: javascript meteor reactjs meteor-react


【解决方案1】:

这令人困惑,因为没有任何东西破坏......并且正在渲染组件,只是没有数据。

扔了几个console.logs,看起来我的容器甚至没有被运行。正如我认为的那样,这很明显啊……在我的 main_layout.js 文件中。

来自

import SidebarHubs from '../components/sidebar_hubs.js';

到:

import SidebarHubs from '../containers/sidebar_hubs.js';

现在就像一个魅力。良好的调试技能大有帮助...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-02
    • 1970-01-01
    • 2019-06-27
    • 1970-01-01
    相关资源
    最近更新 更多