I am trying to fetch data from mongo but the result set is always empty.
The collection I’m trying to access is meteor/offers
. I created the collection in the mongo database myself using a mongodb client. I am connecting to the mongo instance that’s launched when running meteor run
. The collection has 1 document that I created.
I can’t understand why it’s not fetching the document. Do I need to create a Method to fetch the data? I have read through Methods, Collection, and the React example but I’m still having problems.
Also why do I have to create the database myself? The docs say calling Mongo.Collection
“sets up a collection (a storage space for records, or “documents”)”. It doesn’t create the collection on my end hence why I created it myself with the mongo client.
My code is below
imports/api/offers.js
import { Mongo } from 'meteor/mongo'
export const Offers = new Mongo.Collection('offers')
imports/ui/components/OfferListComponent.js
import { Meteor } from 'meteor/meteor'
import React from 'react'
import { withTracker } from 'meteor/react-meteor-data'
import { Offers } from '../../api/offers'
import OfferList from '../components/OfferList'
export default withTracker(({ id }) => {
const handle = Meteor.subscribe('offers', id)
return {
currentUser: Meteor.user(),
loading: !handle.ready(),
offers: Offers.find().fetch(),
}
})(OfferList)