meteor findOne或find返回undefined

时间:2014-03-19 00:35:31

标签: meteor

我在加载主页面之前尝试进行一些验证。要做到这一点,我需要找到一个我已经确认的文件,存在于Mongo Collection中。不幸的是,在client.js中查找文档似乎不起作用。在我看来,客户端和服务器集合不同步。根据我读过的类似文章,我做了很多改变而没有成功。以下是我尝试过的快速摘要。

选项1:尝试在客户端找到记录而不使用自动订阅:记录未找到。

在app.js

credentialToken = "2KcNCRzpTHzyZ1111";

if (Meteor.isClient) {
  Template.hello.greeting = function () {
    return "Welcome to ares_sso.";
  };

  Meteor.startup(function () {    
    var results = Meteor.findrec(credentialToken);  
    console.log("results:",results); //results is undefined.
  });

  Template.hello.events({
    'click input': function () {
       if (typeof console !== 'undefined')
          console.log("You pressed the button");
     }
  });

}

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

在/client/app.js

crs_collection = new Meteor.Collection("crs");
Meteor.subscribe("crs");  

Meteor.findrec = function(credentialToken) {
  target = {credentialtoken:credentialToken};
  recfound = crs_collection.findOne(target);

  //No luck with find either.
  //recfound = crs_collection.find({credentialtoken:credentialToken}, {limit:1}).fetch()[0];
  console.log("recfound:",recfound);  //returns recfound is undefined.
  return recfound;
}

在/server/server.js

crs_collection = new Meteor.Collection("crs");

Meteor.publish("crs", function(){
   return crs_collection.find();
});

选项2:接下来,我使用方法" server_recfind"在服务器端执行了查找。虽然有效,但我无法将内容提供给客户。

在app.js

credentialToken = "2KcNCRzpTHzyZ1111";

if (Meteor.isClient) {
  Template.hello.greeting = function () {
    return "Welcome to ares_sso.";
  };

  Meteor.startup(function () {    
     var results = Meteor.call('server_findrec',credentialToken); 
     console.log("results=",results); // also returns undefined

  });

  Template.hello.events({
    'click input': function () {
       if (typeof console !== 'undefined')
          console.log("You pressed the button");
     }
  });

}

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

在/client/app.js

crs_collection = new Meteor.Collection("crs");
Meteor.subscribe("crs");  

在/server/app.js

crs_collection = new Meteor.Collection("crs");

Meteor.publish("crs", function(){
   return crs_collection.find();
});

// Using Sync which finds the record but how do I sent the content to the client?
Meteor.methods ({
  'server_findrec': function(credentialToken) {
     // tried unblock but didnt work
     //this.unblock();
     var rec = crs_collection.findOne({'credentialtoken': credentialToken});
     console.log("INSIDE server findrec rec=",rec);  //shows content found
     // tried flush but it didn't do anything
     crs_collection.flush;
    return rec;  //rec not returning to the client
  }
})

选项3:感到沮丧,因为我能够使用服务器方法找到文档记录。我尝试添加全局变量以将内容传递到客户端。不幸的是,它没有工作

在app.js

credentialToken = "2KcNCRzpTHzyZ1111";

//added global variables
c1 = '';
c2 = '';
c3 = ''

if (Meteor.isClient) {
  Template.hello.greeting = function () {
    return "Welcome to ares_sso.";
  };

  Meteor.startup(function () {    
     var results = Meteor.call('server_findrec',credentialToken); 
     console.log("results=",results); // also returns undefined
     console.log("c1=",c1);
     console.log("c2=",c2);
     console.log("c3=",c3);  
  });

  Template.hello.events({
    'click input': function () {
       if (typeof console !== 'undefined')
          console.log("You pressed the button");
     }
  });

}

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

在/client/app.js

crs_collection = new Meteor.Collection("crs");
Meteor.subscribe("crs");  

在/server/app.js

crs_collection = new Meteor.Collection("crs");

Meteor.publish("crs", function(){
   return crs_collection.find();
});

// Using Sync which finds the record but how do I sent the content to the client?
Meteor.methods ({
 'server_findrec': function(credentialToken) {

     // tried unblock but didnt work
     //this.unblock();

     var rec = crs_collection.findOne({'credentialtoken': credentialToken});
     console.log("INSIDE server findrec rec=",rec);  //shows content found
     c1 = rec.cont1;
     c2 = rec.cont2;
     c3 = rec.cont3;

     //confirm that c1,c2 and c3 have content
     console.log(In server_findrec c1=",c); //shows content
     console.log(In server_findrec c2=",c2); //shows content
     console.log(In server_findrec c3=",c3); //shows content

     // tried flush to sync to client...didn't work
     crs_collection.flush;

     return rec;  //rec not returning to the client
  }
})

还有更多的代码,所以我已经汇总了以上所有内容,希望它能让你清楚地了解我尝试过的内容以及我尝试做的事情。如果我在这个过程中犯了错误,我很抱歉。

总的来说,知道我做错了什么会很棒吗?我相信这三种情况应该有效。任何帮助或建议将不胜感激。

我使用的是Meteor版本0.7.1.2,没有使用CoffeeScript。

谢谢大家

1 个答案:

答案 0 :(得分:1)

你犯了两个错误:

  1. 定义crs_collection一次,并确保它在客户端和服务器上执行的文件中。它应该在全球范围内定义。

  2. crs_collection必须在您的pub / sub代码之前定义。 Meteor首先执行lib目录中的文件,因此最好将收集代码放在那里。

  3. 这就是它的全部内容。如果需要,我很乐意提供一个例子。