1

I have a Meteor Application that I'm developing with React. I still have the autopublish package in my project (autopublish@1.0.7).

Here is my relevant code:

MainMenu.jsx

import React, { Component, PropTypes } from 'react'
import { Meteor } from 'meteor/meteor'
import { FlowRouter } from 'meteor/kadira:flow-router'
import { createContainer } from 'meteor/react-meteor-data'

import { ChatRooms } from '/imports/api/chatrooms.js'

export class MainMenu extends Component {
    render() {
        console.log(this.props.chatrooms)

        return (
            {/* Render stuff here is not part of the scope of this question */}
        )
    }
}

MainMenu.PropTypes = {
    chatrooms: PropTypes.array.isRequired
}

export default createContainer(() => {
    return {
        chatrooms: ChatRooms.find({}).fetch()
    }
}, MainMenu)

chatrooms.js

import { Mongo } from 'meteor/mongo'

export const ChatRooms = new Mongo.Collection('chatrooms')

The console.log(this.props.chatrooms) in the MainMenu Component always returns an empty array ([]).

There are definitely items in the Mongo Database because when I run the meteor mongo command in my console and type db.chatrooms.find({}); it returns the 3 items that I've inserted to test this all.

Anyone have any idea what I may be doing wrong here? Help would be much appreciated!

Barry Michael Doyle
  • 9,333
  • 30
  • 83
  • 143
  • 1
    your code snippet in the question helped me fetching data in my first meteor+react+mongo app that I was unable to achieve from the last 4+ hours, thanks :) – Adil Aug 04 '17 at 11:33
  • 1
    Glad this helped you out @adi! :) – Barry Michael Doyle Aug 04 '17 at 12:35
  • I've observed a strange thing that my code doesn't work without this snippet in the first class: `componentDidMount() { Meteor.subscribe('projects'); }` – Adil Aug 04 '17 at 13:47

1 Answers1

2

I've figured it out. I left out a crucial step of this whole process.

In my /server/main.js file I needed to add the following line which fixed everything:

import '../imports/api/chatrooms.js
Barry Michael Doyle
  • 9,333
  • 30
  • 83
  • 143