简体   繁体   English

流星+ React:无法从mongoDB集合中获取数据

[英]Meteor + React: Can't get data from mongoDB Collection

I have a Meteor Application that I'm developing with React. 我有一个正在使用React开发的Meteor应用程序。 I still have the autopublish package in my project ( autopublish@1.0.7 ). 我的项目中仍然有autopublish包( autopublish@1.0.7 )。

Here is my relevant code: 这是我的相关代码:

MainMenu.jsx 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 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 ( [] ). MainMenu组件中的console.log(this.props.chatrooms)始终返回一个空数组( [] )。

There are definitely items in the Mongo Database because when I run the meteor mongo command in my console and type db.chatrooms.find({}); Mongo数据库中肯定有项目,因为当我在控制台中运行meteor mongo命令并键入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! 帮助将不胜感激!

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: 在我的/server/main.js文件中,我需要添加以下行来修复所有问题:

import '../imports/api/chatrooms.js

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM