espenk
espenk

Reputation: 585

Aplication crashing when using Session in meteor

I just started learning the Meteor framework using the book getting started with meteor.js javascript framework

I follow along the examples there and now I have this code in my.js file

lists = new Meteor.Collection("Lists");
Session.set("adding_category", false);

if (Meteor.isClient) {
 Template.categories.lists = function() {
   return lists.find({}, {sort: {Category: 1}});
 }

 Template.categories.new_cat = function() {
  return Session.equals("adding_category", true)
 }

 Template.categories.events({
  'click #btnNewCat': function (e,t) {
      Session.set('adding_category',true);
      Meteor.flush();
      focusText(t.find("#add-category"));
  }
 }) 

function focusText(i) {
  i.focus();
  i.select();
};
}

if (Meteor.isServer) {
  Meteor.startup(function () {
    // code to run on server at startup
  });
}

Now my app crashes:

Your app is crashing. Here's the latest log.

W2033-10:54:24.755(0)? (STDERR) /vagrant/mySendondMeteroApp/.meteor/local/build/programs/server/boot.js:184 W2033-10:54:24.757(0)? (STDERR) }).run(); W2033-10:54:24.757(0)? (STDERR)    ^ W2033-10:54:24.761(0)? (STDERR) ReferenceError: session is not defined W2033-10:54:24.761(0)? (STDERR)     at app/mySendondMeteroApp.js:2:1 W2033-10:54:24.762(0)? (STDERR)     at app/mySendondMeteroApp.js:33:3 W2033-10:54:24.763(0)? (STDERR)     at mains (/vagrant/mySendondMeteroApp/.meteor/local/build/programs/server/boot.js:153:10) W2033-10:54:24.763(0)? (STDERR)     at Array.forEach (native) W2033-10:54:24.764(0)? (STDERR)     at Function._.each._.forEach (/home/vagrant/.meteor/tools/3cba50c44a/lib/node_modules/underscore/underscore.js:79:11) W2033-10:54:24.765(0)? (STDERR)     at /vagrant/mySendondMeteroApp/.meteor/local/build/programs/server/boot.js:80:5
=> Exited with code: 1
=> Your application is crashing. Waiting for file change.

Upvotes: 0

Views: 1116

Answers (1)

Konstantin K
Konstantin K

Reputation: 1317

Try putting Session.set("adding_category", false); inside your if (Meteor.isClient) { . Otherwise it's run both client- and server-side while the server has no idea what "Session" is and throws a reference error on line 2 of your app source file:

(STDERR) ReferenceError: session is not defined W2033-10:54:24.761(0)? (STDERR) at app/mySendondMeteroApp.js:2:1

Update: here's some code for you

lists = new Meteor.Collection("Lists");

//Session.set("adding_category", false); // This would be run both on the client and the server. Don't do it, the server doesn't know what Session is.

if (Meteor.isClient) {
 Session.set("adding_category", false); // <<<<<<<< now it's run only on the client!

 Template.categories.lists = function() {
   return lists.find({}, {sort: {Category: 1}});
 }

 Template.categories.new_cat = function() {
  return Session.equals("adding_category", true)
 }

 Template.categories.events({
   'click #btnNewCat': function (e,t) {
      Session.set('adding_category',true);
      Meteor.flush();
      focusText(t.find("#add-category"));
   }
 })

  function focusText(i) {
    i.focus();
    i.select();
  };
}

//the rest of your code ....

Upvotes: 1

Related Questions