在流星中使用Session时应用程序崩溃

时间:2013-10-10 11:14:54

标签: javascript meteor

我刚开始使用开始使用meteor.js javascript框架

开始学习Meteor框架

我按照那里的例子,现在我在my.js文件中有这个代码

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
  });
}

现在我的应用程序崩溃了:

您的应用崩溃了。这是最新的日志。

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.

1 个答案:

答案 0 :(得分:1)

尝试将Session.set("adding_category", false);放入if (Meteor.isClient) {。否则它会在客户端和服务器端运行,而服务器不知道“会话”是什么,并在应用程序源文件的第2行引发引用错误:

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

更新:这里有一些代码

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 ....