【问题标题】:Meteor template helper to check if a document existsMeteor 模板助手检查文档是否存在
【发布时间】:2016-10-01 15:50:24
【问题描述】:

我想注册一个助手,我可以在模板中使用它来检查文档是否存在。

我检查是否存在这样的文档

var selector = {
    userid: "3R2pKdT3x9PjWLsD8",
};;

var this_exists = Af.find(selector, {limit: 1}).count() > 0;

我正在尝试注册这样的助手

Template.registerHelper('ex', function() {
    var selector = {
        userid: "3R2pKdT3x9PjWLsD8",
    };

    var this_exists = Af.find(selector, {limit: 1}).count() > 0;

    if(this_exists == true) {
        return true;
    } else {
        return false;
    }
});

在我的模板中像这样使用它

{{#if ex}}
    {{> quickForm collection="Af" doc=cdoc id="updateSettings" omitFields="userid" class="settings" type="update" buttonContent="Update Settings"}}
{{else}}
    {{> quickForm collection="Af" doc=this id="updateSettings" omitFields="userid" class="settings" type="insert" buttonContent="Insert Settings"}}
{{/if}}

但这不起作用。我哪里错了?

【问题讨论】:

  • 您的代码乍一看很合理。你能指定它“不起作用”的方式吗?
  • 原来是chrome缓存了我使用了很长时间的某个库,所以,调试窗口正在调试缓存的版本,它不是正在使用的版本,但仍然设法隐藏了我使用的库有问题。

标签: meteor handlebars.js meteor-blaze


【解决方案1】:

如果这不能解决您的问题,那么您的发布/订阅可能存在问题。

你不需要限制。而是使用findOne,您不需要检查计数。

var exists = Af.findOne({userId: "3R2pKdT3x9PjWLsD8"}); //be careful with userId here, as it might be userid in your codes. 

If(exists){
    return true;
} //we don't need else block here. Your html if else block will work fine with this.

另外,如果你使用的userId是当前用户的id,你可以使用Meteor.userId()

var exists = Af.findOne({userId: Meteor.userId()});

【讨论】:

    猜你喜欢
    • 2015-01-06
    • 1970-01-01
    • 2015-09-20
    • 2015-09-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-19
    • 2014-06-20
    相关资源
    最近更新 更多