Meteor:IF语句返回true但页面显示false(显示用户Facebook图片)

Meteor:IF语句返回true但页面显示false(显示用户Facebook图片),meteor,Meteor,我试图在facebook用户登录时显示facebook图片,在非facebook用户登录时显示占位符 我已在我的用户页面html中创建了if语句: {{#if facebookuserloggedin}} TRUE <img src="http://graph.facebook.com/{{facebookuser}}/picture/picture?type=large" alt="{{user}}"> {{else}

我试图在facebook用户登录时显示facebook图片,在非facebook用户登录时显示占位符

我已在我的用户页面html中创建了if语句:

        {{#if facebookuserloggedin}}
        TRUE
        <img src="http://graph.facebook.com/{{facebookuser}}/picture/picture?type=large" alt="{{user}}">
        {{else}}
        False
        <img src="user.png" alt="stanley">
        {{/if}}
        <h1>Welcome {{user}}</h1>

出于某种原因,该警报显示帮助程序正在返回true(当facebook用户登录时),但html页面显示false。正因为如此,占位符被显示出来,而不是facebook的个人资料图片。我真的不知道这是怎么发生的。有人能帮我吗?提前谢谢

正如@apendua所说,helper函数是同步的,而您调用的是异步函数。简而言之,这意味着您编写的
return
语句不是针对helper函数的返回语句,而是针对传递给
FB.getLoginStatus
的函数的返回语句。请注意,您使用了
function
关键字两次,因此这里有两个函数,而return语句的位置是错误的

这是初学者常见的陷阱,所以别担心,有一个简单的解决方法。它需要将您的状态存储在另一个位置(如局部变量)并对其进行反应性监视

这里可以使用的模式很少。首先,我建议在
rendered
回调中调用异步函数,并为结果创建单独的依赖项。这会让你对正在发生的事情有最好的了解

var status = null
var statusDep = new Deps.dependency();

Template.templateName.rendered = function() {
    FB.getLoginStatus(function(response) {
        ...
        if(...) {
            status = true;
            statusDep.changed();
        } else {
            status = false;
            statusDep.changed();
        }
    });
};

Template.templateName.helpers({
    facebookuserloggedin: function() {
        statusDep.depend();
        return status;
    };
});

从这里显示的内容来看,您的助手似乎总是返回未定义的
。内部回调是异步调用的,对吗?什么是内部回调异步?我是一个初学者,所以不完全理解这个概念。警报显示为true,页面显示为false。感谢您的帮助!这说明了很多问题。如果您有时间,可以快速解释statusDep.depend()的作用吗?简言之:无论何时调用
statusDep.changed()
代码中的任何位置,它都会导致函数重新计算。有关更详细的解释,请阅读此处:。
var status = null
var statusDep = new Deps.dependency();

Template.templateName.rendered = function() {
    FB.getLoginStatus(function(response) {
        ...
        if(...) {
            status = true;
            statusDep.changed();
        } else {
            status = false;
            statusDep.changed();
        }
    });
};

Template.templateName.helpers({
    facebookuserloggedin: function() {
        statusDep.depend();
        return status;
    };
});