0

我尝试使用具有一些方法的对象集合。

IE :

Targets = new Meteor.Collection("targets");
Targets.insert({
  id : 0,
  title : function(){
    if(false){
      console.log("true");
      return 'Le commencement';
    }
    else
    {
      console.log("false");
      return 'Le début';
    }
  },
  text : 'Lorem ipsum dolor.',
});`

我稍后用

Template.content.target = function() {
  var currentPosition = Meteor.user().profile.position;
  return Targets.findOne({id: parseInt(currentPosition)});
};

在我的模板中:

{{#if currentUser}}
  <h2>{{target.title}}</h2>
  <p>{{target.text}}</p>
{{/if}}

流星反应是完全随机的......

有时我在控制台中得到“假”,标题是“Le début”。有时我得到“Le开始”作为标题,而控制台中什么也没有。有时我在控制台中两次获得“假”,并在几秒钟内获得“Le début”作为标题,然后它消失了......

我不知道该怎么办...

也许只是存储在 mongo 中的对象中的方法是一个非常糟糕的想法。

感谢帮助。

4

1 回答 1

0

Mongodb是一个数据库,你不应该在里面放一些逻辑代码。因此,您需要提取对象的属性,然后在集合中使用转换函数。像这样:

Targets = new Meteor.Collection("targets", {
    transform: function(doc) {
        return new Target(doc)
    }
});

或者,如果您不想创建Target对象:

Targets = new Meteor.Collection("targets", {
    transform: function(doc) {
        doc.title = function(){
            if(false) {
                console.log("true");
                return 'Le commencement';
            } else {
                console.log("false");
                return 'Le début';
            }
        return doc
        }
    }
});
于 2013-06-18T17:38:12.610 回答