Reputation: 1765
Two weeks ago I started to learn courses on coursera and there is an example where it shows how to use the Meteor.isClient
method with console.log
and Mongo. But it didn't work. My Windows CLI doesn't show anything in the output even after restart and if I try in the browser console type console.log(Images.find().count())
it outputs 0.
Images = new Mongo.Collection("images");
if (Meteor.isServer){
Meteor.startUp(function(){
if(Images.find().count() == 0){
Images.insert({
img_src:'1.jpg',
img_alt:'Here i am !'
});
} //end of if have no images
});
}
console.log('startup : ' + Images.find().count());
Upvotes: 0
Views: 72
Reputation: 3240
Meteor.isServer
works fine in windows, though for a long time now it has been recommended that rather than use Meteor.isServer
and Meteor.isClient
you should split the code into /client
and /server
directories.
With new versions meteor, when you run meteor create testApp
this following is created:
All code in /client
is only run on the the client, and all code in /server
is only run on the server.
Creating/Editing these initial files to include the following log commands shows both where (client/server) and in which order the code is run:
/client/main.js
import { Template } from 'meteor/templating';
import { ReactiveVar } from 'meteor/reactive-var';
import './main.html';
Template.hello.onCreated(function helloOnCreated() {
// counter starts at 0
this.counter = new ReactiveVar(0);
});
Template.hello.helpers({
counter() {
return Template.instance().counter.get();
},
});
Template.hello.events({
'click button'(event, instance) {
// increment the counter when button is clicked
instance.counter.set(instance.counter.get() + 1);
},
});
Meteor.startup(() => {
console.log('Ping! from /client/main.js - Meteor.startup()')
});
console.log('Ping! from /client/main.js - Top Level')
/server/main.js
import { Meteor } from 'meteor/meteor';
Meteor.startup(() => {
console.log('Ping! from /server/main.js - Meteor.startup()')
});
console.log('Ping! from /server/main.js - Top Level')
/shared.js
// Runs on Both
console.log('Hi from /shared.js - Top Level')
Meteor.startup(() => {
console.log('Hi from /shared.js - Meteor.startup()')
});
// Runs on Server
if(Meteor.isClient){
console.log('Hi from /shared.js - isClient')
Meteor.startup(() => {
console.log('Hi from /shared.js - isClient, Meteor.startup()')
});
}
// Runs on Server
if(Meteor.isServer){
console.log('Hi from /shared.js - isServer')
Meteor.startup(() => {
console.log('Hi from /shared.js - isServer, Meteor.startup()')
});
}
/lib/shared.js
console.log('Ping! from /lib/shared.js - Top Level')
Meteor.startup(() => {
console.log('Ping! from /lib/shared.js - Meteor.startup()')
});
Here are the resulting logs from the server and browser:
On both the Client and Server, the console.log
lines not in Meteor.startup
blocks run first, followed by the Meteor.startup
callbacks, because these are delayed until the server process has finished starting, or the DOM is ready. They are executed in the same order in which the calls to Meteor.startup
were made.
The calls not in a Meteor.startup
block are executed as their files are loaded, following the Default file load order.
Upvotes: 0
Reputation: 1383
First, your console.log
is outside of your Meteor.isServer
so it is pretty normal that it shows up in the client.
Second, if you want to show your log in the server, you have to either put it in the startUp
function or in a method that you call in the client.
Upvotes: 2