集合findOne正在返回undefiend

时间:2016-05-24 08:11:42

标签: mongodb meteor

此Meteor客户端代码应返回一个在服务器控制台上正常工作的文档。 db.validationCol.findOne({'key':'myname'});

但是在客户端返回undefiend。怎么修好?谢谢

// server
ValidationCol = new Mongo.Collection('validationCol');

Meteor.publish('validationCol', function () {
  return ValidationCol.find({});
});

(function () {
  const docs = {
    myname: '^[0-9a-zA-Z]{65}$'
  };
  for (let property in docs) {
    if (docs.hasOwnProperty(property)) {
      ValidationCol.insert({key: property, regex: docs[property]});
     }
  }
})();

//client
let key = 'myname'
      let regex = ValidationCol.findOne({key: key});
      console.log(regex);

1 个答案:

答案 0 :(得分:0)

您必须在客户代码中订阅:

ValidationCol = new Mongo.Collection('validationCol');

Meteor.publish('validationCol', function () {
  return ValidationCol.find({});
});

(function () {
  const docs = {
    myname: '^[0-9a-zA-Z]{65}$'
  };
  for (let property in docs) {
    if (docs.hasOwnProperty(property)) {
      ValidationCol.insert({key: property, regex: docs[property]});
     }
  }
})();

//client
let key = 'myname'
// Subscription
Meteor.subscribe('validationCol');
let regex = ValidationCol.findOne({key: key});
console.log(regex);