Subscription not working - where is the error?

Hello,
I am having the “Uncaught ReferenceError: Userdata is not defined” in my client console.

My code:

client/main.js:

import { Userdata } from '../custom/script.js';
Meteor.subscribe('Userdata');

custom/script.js:

import { Mongo } from 'meteor/mongo';
export const Userdata = new Mongo.Collection('Userdata');

server/main.js:

import { Meteor } from 'meteor/meteor';
import '../custom/script.js'; 

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

In your server main.js you don’t import Userdata. Use the same syntax you used in client main.js

1 Like

That way?

server/main.js:

import { Meteor } from 'meteor/meteor';
import '../custom/script.js'; 
import { Userdata } from '../custom/script.js';

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

Unfortunately - the same error. I am typing in my client console:

Userdata.find().fetch()

You cannot use these variables in the client console, since they are not exposed to the global namespace (i.e. not bound to the window object).

If you want to test it like this, you would have to set them as a property of the window object explicitly:

window.Userdata = Userdata;

in your client file. When you’re done testing, you should remove this, to avoid polluting the global namespace.

(You should also get rid of the double-import in server/main.js)

When I issue in my client console:
window.Userdata = Userdata;

I am getting the same error (Userdata not defined).

Please note that worked well when the Autopublish was turned on.

You have to put this code line in your client file:

import { Userdata } from '../custom/script.js';
Meteor.subscribe('Userdata');
window.Userdata = Userdata;

This will make Userdata available in your console.

Works! Thank you. I would not figure it out.

1 Like

Have another strange problem.
My publish is now:

Meteor.publish('Userdata', function() {
  	console.log("I am inside publish");
	console.log("this.userId: "+this.userId )
	if(!this.userId){
		throw new Meteor.Error('not authorized');
		return false;		
	} else {
  			console.log("I am inside if");
		return Userdata.find({owner: this.userId});
	} 
});

The problem is that when I log out and then login it doesn’t enter the publish function anymore and the Userdata is empty. It stays that way up to the moment I refresh the whole page.

So:
So on startup when I refresh the page and not logged in I get:
I am inside publish
this.userId: null

I login - and nothing. Up to the point when I refresh the page, then there is:
I am inside publish
this.userId: EyDfvF8DgysNPYpvE
I am inside if

OR

I am logged in, I refresh the page, I get:
I am inside publish
this.userId: EyDfvF8DgysNPYpvE
I am inside if

I logout, I get:

I am inside publish
this.userId: null

I login again to the same or another account and… nothing. And from that point no matter how many logins, and logouts, it does not enter publish anymore. Up to the point I hit F5 to refresh the page on the client.

etc… totaly random and unreliable. Why can that be?

This is because you Meteor.subscribe is not reactive in itself, i.e. it won’t notice if your user has changed. Try changing your client file to this:

import { Userdata } from '../custom/script.js';
import { Meteor } from 'meteor/meteor';
import { Tracker } from 'meteor/tracker';

Tracker.autorun(() => {
   const userId = Meteor.userId();
   Meteor.subscribe('Userdata');
});

window.Userdata = Userdata;

Meteor.userId() is a reactive source, which means it causes the autorun to re-run once the user changes. Which in turn lets the subscription re-run.

Note that normally you would do this inside a client’s UI code, i.e. inside a Blaze, React, or Vue component. The way you do it there differs from the solution above and is dependent on the UI library. In React, for instance, you would use withTracker.

I’d strongly recommend that you work through one of the excellent Meteor tutorials instead of trying to figure things out on your own:

https://www.meteor.com/tutorials

Pick the To Do tutorial first, and maybe use the Blaze version as Blaze is much easier to understand than React.

After you have finished the tutorial, continue reading the Meteor guide:

To understand the API in depth, you should also have a look at the API docs here:

https://docs.meteor.com/

2 Likes

Got it. Thank you. ------------------