Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/meteor/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angularjs 如何在angular meteor和ionic中反应性地获取当前登录的用户?_Angularjs_Meteor_Ionic_Angular Meteor - Fatal编程技术网

Angularjs 如何在angular meteor和ionic中反应性地获取当前登录的用户?

Angularjs 如何在angular meteor和ionic中反应性地获取当前登录的用户?,angularjs,meteor,ionic,angular-meteor,Angularjs,Meteor,Ionic,Angular Meteor,我希望$scope.me以反应方式表示当前登录的用户,以便当用户注销并作为另一个用户重新登录时,此变量将更新。现在,当一个用户注销并作为另一个用户重新登录时,$state.me的旧值仍然存在。重新加载页面后,将更正此值。我该如何解决这个问题 这是我在控制器中工作的糟糕解决方案: $scope.$on('$ionicView.enter', function(e) { if($rootScope.currentUser) $scope.me = $rootScope.cur

我希望
$scope.me
以反应方式表示当前登录的用户,以便当用户注销并作为另一个用户重新登录时,此变量将更新。现在,当一个用户注销并作为另一个用户重新登录时,
$state.me
的旧值仍然存在。重新加载页面后,将更正此值。我该如何解决这个问题

这是我在控制器中工作的糟糕解决方案:

$scope.$on('$ionicView.enter', function(e) {
    if($rootScope.currentUser)
        $scope.me = $rootScope.currentUser;

});
这是可行的,但每次用户转换到此状态时都会重置变量。。。丑陋的非流星解决方案

以下是我目前的尝试:

$scope.me = ($scope.$meteorCollection(function(){
    return Meteor.users.find({_id: Meteor.userId()});
}))[0];
这应该是可行的,因为Meteor.userId()是被动的,当它发生变化时,应该强制它的父函数重新运行,从而实时更正
$scope.me
。。。但事实并非如此

相反,
$scope.me
将更新为旧用户的净化配置文件。。。这意味着除了它们的
\u id
配置文件
之外,什么都看不见。这告诉我,
$scope.$meteorCollection
正在重新运行,但旧值为
Meteor.userId()

我错过了什么?谢谢大家!

*编辑* 这是一个转折点

$scope.me = ($scope.$meteorCollection(function(){
    console.log("$scope.me reset to "+Meteor.userId());
    return Meteor.users.find({_id: Meteor.userId()});
}))[0];

当用户切换时,将新用户的ID打印到控制台,但即使使用正确的值重新运行查询,仍会返回旧用户。

您是否尝试使用
$rootScope.currentUser
而不是尝试烘焙您自己的解决方案?角流星,医生说它是反应性的

另一种可能的解决方案是使用
$meteor.autorun
便利方法,但不是基于$scope变量中的更改自动运行,您可以使用类似
if(meteor.userId())
if(meteor.user())
的方法

事实上,这就是角流星在掩体下所做的,如果你想知道的话

从:


我找到了一个解决方案:

$meteor.autorun($scope, function(){
    var user = (Meteor.users.find({_id: Meteor.userId()}).fetch())[0];
    if( user != null ){
        $scope.me = user;
    }
});
自动重新运行函数中包含的反应依赖项。在本例中,
Meteor.userId()
。因此,只要meteor对其进行了更改,函数体就会重新运行,并在登录时将
$scope.me
设置为当前用户


谢谢你给JacobWuzHere的提示

谢谢你详细的回答<代码>$scope.me=$rootScope.currentUser是我尝试的第一件事,实际上并没有反应性地更新,我必须在以新用户身份登录后重新加载应用程序,以使值自动更正。奇怪的是,考虑到源代码,这种情况不应该发生……重新加载应用程序似乎是一种昂贵的解决问题的方法。您是否尝试过使用帐户系统提供的重新设置$scope.me?根据您使用的路由器,您可能能够在创建作用域之前强制解析某些变量或服务。在这种情况下,您可以使用方便的方法
$meteor.waitForUser()
$rootScope.logging
作为重新分配给$scope.meA固溶体的提示,我唯一的问题是这是非反应性的。如果用户的信息在登录时发生更改,这些更改将不会反映出来,因为没有登录挂钩可以触发。好的一点——原因是我没有在
$scope.getreactive()
语句中检查深度相等。我正在用修改后的解决方案更新我的答案——很好奇它是否适合你。我认为这可能是更高的性能考虑到你下面的答案似乎是重复的一些工作,角流星已经为我们所做的。
$meteor.autorun($scope, function(){
    var user = (Meteor.users.find({_id: Meteor.userId()}).fetch())[0];
    if( user != null ){
        $scope.me = user;
    }
});