Meteor React - 发布/订阅不起作用

时间:2017-11-06 11:43:31

标签: javascript mongodb reactjs meteor

我无法弄清楚我哪里出错了,但这是我的导入代码/ api

import { Mongo } from 'meteor/mongo';

Blogposts = new Mongo.Collection('blogposts');

if (Meteor.isServer) {

  Meteor.publish('allBlogposts', function () {
    return Blogposts.find({});
  });
}

在我的前端jsx文件中:

import React from 'react';
import ReactDOM from 'react-dom';
import TrackerReact from 'meteor/ultimatejs:tracker-react';

Blogposts = new Mongo.Collection('blogposts');

 export default class BlogPage extends TrackerReact(React.Component) {

constructor(props) {
super(props);
this.state = {
  subscription: {
    blogposts: Meteor.subscribe('allBlogposts')
  }
}
}

从那里开始,如果我控制日志,我似乎无法从我的数据库访问任何数据,我从服务器向数据库添加了两条记录。

如何才能使这个用于返回这两个文档的简单控制台日志?

1 个答案:

答案 0 :(得分:0)

# api    
import { Mongo } from 'meteor/mongo';

export default Blogposts = new Mongo.Collection('blogposts');

if (Meteor.isServer) {

   Meteor.publish('allBlogposts', function () {
      return Blogposts.find({});
   });
}

Component

import React, { Component } from 'react';
import ReactDOM from 'react-dom';
import PropTypes from 'prop-types';
import { withTracker } from 'meteor/react-meteor-data';

#import your api
import { Blogposts } from "whatever/the/dir/is.js

class BlogPage extends Component {

# Everything here

}
#you write this function outside your component
BlogPage.propTypes = {
  currentUserPost: PropTypes.array.isRequired,
};
export default withTracker(()=>{
  Meteor.subscribe("allBlogposts");
  return {
    currentUserPost: Blogposts.find()
  }
})(BlogPage);