0

我刚开始使用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.
4

1 回答 1

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 ....
于 2013-10-10T11:22:42.027 回答