Collection from subscription undefined

Hello,

I’m new to MeteorJS and I’m trying to subscribe to a collection from a Tracker.
Here is my code :

...
Companies = new CompaniesCollection("companies");
...

Meteor.publish("companies", () => {
	return Companies.find({});
});

(Server Side)

export default withTracker(props => {
	var handle = Meteor.subscribe("companies");

	if (handle.ready()) {
		console.log("Ready !");
		console.log(Companies.find({}));
	} else {
		console.log("Not ready");
	}

	return {};
})(CompaniesManager);

(Client Side)

It seems that the publish works well, since I get the “Ready”, but when I’m trying to access my collection using the cursor, I get “Companies undefined”…

Thank you in advance for any kind of help :blush:

Yardak

Can you show your complete code? I’m not sure what you mean by you get “Companies undefined”.

Sure, first here is the error I get :

Uncaught ReferenceError: Companies is not defined
...
Exception from Tracker recompute function:
ReferenceError: Companies is not defined

They concerns the line I do “console.log(Companies.find())”

And here is my complete code for server side :

import { Meteor } from "meteor/meteor";

class CompaniesCollection extends Mongo.Collection {
	insert(company, callback) {
		Companies.schema.validate(company);

		return super.insert(company, (err, _id) => {
			CompaniesProfile.insert({ company_id: _id });
			CompaniesSettings.insert({ company_id: _id });
		});
	}
}

Companies = new CompaniesCollection("companies");

Companies.schema = new SimpleSchema({
	ticker: { type: String }
});

Meteor.methods({
	"admin.company.add": function(data) {
                // Just hide this one, I guess its not releavant
	}
});

Meteor.publish("companies", () => {
	return Companies.find({});
});

For the server side I have nothing more except my Component CompaniesManager, which is a basic React component, should I import any file concerning my collection ?

import React, { Component } from "react";
import PropTypes from "prop-types";
import { Meteor } from "meteor/meteor";
import { withTracker } from "meteor/react-meteor-data";
import { BrowserRouter as Router, Switch, Route } from "react-router-dom";

import { Row, Col } from "react-flexbox-grid";

Thank you :slight_smile:

Looks like you aren’t importing your the Companies collection.

1 Like

I dont get it, where should I import it ? In my client file ? Isnt that supposed to remain on server part ?

Yes, you either have to import it in your client file or you have to define a collection by the same name somewhere on the client and import it to your current file. Otherwise the client won’t have anywhere to store the documents that get published by the server and you won’t have a reverence to the Collection in your components.

1 Like

Thank you so much :sunny:

No problem at all :slight_smile: