首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >React Komposer,react and Meteor

React Komposer,react and Meteor
EN

Stack Overflow用户
提问于 2016-08-16 18:13:00
回答 1查看 173关注 0票数 2

我正在使用react Komposer meteor和react。我有这个组件

代码语言:javascript
复制
import React from 'react';
import getMuiTheme from 'material-ui/styles/getMuiTheme';
import lightBaseTheme from 'material-ui/styles/baseThemes/lightBaseTheme';
import MuiThemeProvider from 'material-ui/styles/MuiThemeProvider';

const lightMuiTheme = getMuiTheme(lightBaseTheme);

const Questiondetails = ({ thequestion }) => (
  <div>
    <MuiThemeProvider muiTheme={lightMuiTheme}>
      <h4>{thequestion.header}</h4>
    </MuiThemeProvider>
  </div>
);


export default Questiondetails;

这是一个容器

代码语言:javascript
复制
import { Meteor } from 'meteor/meteor';
import React from 'react';
import { composeWithTracker } from 'react-komposer';
import CircularProgress from 'material-ui/CircularProgress';
import darkBaseTheme from 'material-ui/styles/baseThemes/darkBaseTheme';
import MuiThemeProvider from 'material-ui/styles/MuiThemeProvider';
import getMuiTheme from 'material-ui/styles/getMuiTheme';
import Questiondetails from '../../../ui/components/Questiondetails/Questiondetails.jsx';
import Questions from '../../Collections/Questions/Questions.js';

function composer(props, onData) {
  const handle = Meteor.subscribe('singleQuestion', props._id);

  if (handle.ready()) {
    const thequestion = Questions.findOne({ id: props._id });
    onData(null, { thequestion });
  }
}

const darkMuiTheme = getMuiTheme(darkBaseTheme);

const MyLoading = () => (<div style={{ width: '90%', position: 'relative'   }}>
  <MuiThemeProvider muiTheme={darkMuiTheme}>
    <div style={{ margin: 'auto', right: 0, left: 0, maxWidth: 200, position: 'relative' }}>
      <CircularProgress size={1.0} />
    </div>
  </MuiThemeProvider>
</div>);

export { MyLoading };

export default composeWithTracker(composer, MyLoading)(Questiondetails);

我在问Exception from Tracker recompute function: debug.js:41TypeError: Cannot read property 'header' of undefined我能做什么。当我看着流星玩具时。我可以在组件中看到订阅。

这是我的出版物

代码语言:javascript
复制
import { Meteor } from 'meteor/meteor';

// import the db
import Questions from '../../../../api/Collections/Questions/Questions.js';

// the publish
Meteor.publish('singleQuestion', function(id){
  return Questions.find({ _id: id });
});
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-08-16 20:19:53

很可能你没有得到数据记录。

即使在订阅handle准备就绪之后,查询也有可能返回undefined,因为集合中没有数据,或者查询是错误的。

在这种情况下,查询似乎确实是错误的,导致您将undefined传递给组件,而不是预期的对象。

如果您提供一个字符串作为find()findOne()的第一个参数,则假定您指的是_id,因此它可以防止出现类似于您犯下的(常见)错误(Questions.findOne({ id: props._id }),使用id键而不是_id)。

您可以使用error参数,以便更容易地捕获此类情况(并在出现实际错误时显示有意义的错误消息)。

我还建议将thequestion更改为questiontheQuestion (可读性更好),除非有很好的理由不这样做。

代码语言:javascript
复制
function composer(props, onData) {
  const handle = Meteor.subscribe('singleQuestion', props._id);

  if (handle.ready()) {
    const question = Questions.findOne(props._id);
    let error = null;
    if (!question) {
      error = new Error('no question matches the provided id');
    }
    onData(error, {question});
  }
}
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38972184

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档