Meteor.js中的函数在封装在另一个函数中时总是返回False



Meteor.js中有一个函数,当我将它封装在另一个函数中时,它的行为与预期不一样。在下面提供的示例中,我使用了两次"if-OnlyBank(("函数:在一个例子中,"if-ollyBank("函数只是放在HTML中。(见图1(这是有效的,并按预期返回True/False。然而,当我把这个函数放在另一个函数(特别是"if-currentUser"函数(中时,它总是返回false。(见图2(我怀疑这可能是本地/全局范围的问题,但我不知道如何解决这个问题。有人能提供帮助吗?

我是流星和堆栈溢出的新手。

Javascript

Template.body.helpers({
onlyBank() {
return Banks.findOne({userId: Meteor.userId()}, Banks.accountNumber:{"$exists":false}, Banks.routingNumber:{"$exists": false});
},
});

HTML

<body>
<div class="container">
{{> loginButtons}}
{{#if onlyBank}}
True
{{else}}
False
{{/if}} <!-- Figure 1. This if statement will retun True when the onlyBank's conditions are met -->
<div class="row">
{{#if currentUser}}
<div class="col s8">
{{#if onlyBank}}
True
{{else}}
False
{{/if}} <!-- Figure 2. This if statement ALWAYS returns false -->
</div>
<div class="col s4">
{{> banks}}
</div>
{{else}}
YOU NEED TO BE LOGGED IN FIRST
{{/if}}
</div>
</div>
</body>

如果我做对了,Meteor.userId就不是一个函数。

您应该使用:{userId: Meteor.userId}

也许你可以试试:

Template.body.helpers({
onlyBank() {
return (Meteor.userId !== undefined) && (Banks.findOne({userId: Meteor.userId}, Banks.accountNumber:{"$exists":false}, Banks.routingNumber:{"$exists": false}) !== undefined);
},
});

这将始终返回一个布尔值

最新更新