简体   繁体   English

角全栈获取当前用户ID

[英]Angular-fullstack get current user ID

I'm using https://github.com/DaftMonk/generator-angular-fullstack . 我正在使用https://github.com/DaftMonk/generator-angular-fullstack

I have 2 schemas: 我有2个架构:

  • charts 图表
    • title: String 标题:字符串
    • datasets: Array 数据集:数组
    • _creator: logged in user ID _creator:登录的用户ID
  • users 使用者
    • name 名称
    • email 电子邮件
    • _id _ID

When I console.log the current user returns 当我console.log时,当前用户返回 在此处输入图片说明

$scope.getCurrentUser = Auth.getCurrentUser();        
console.log($scope.getCurrentUser);

But when I try to console log the _id: 但是当我尝试控制台_id时:

$scope.getCurrentUser = Auth.getCurrentUser();        
console.log($scope.getCurrentUser._id);

Returns 退货

undefined 未定义

Can someone explain what I'm doing wrong? 有人可以解释我在做什么错吗?

Objects in chrome console get evaluated only when they are first opened. chrome控制台中的对象仅在首次打开时才进行评估。

This means that when you console.log an object like the return value of Auth.getCurrentUser() , the console displays a reference to it - this at the time of the log call contains a promise object, but it's most likely resolved by the time you open it on the console, so you see the property you're looking for. 这意味着,当您console.log一个对象(如Auth.getCurrentUser()的返回值Auth.getCurrentUser() ,控制台将显示对该对象的引用-在日志调用时此对象包含一个promise对象,但很可能在该时间之前已解决您可以在控制台上将其打开,从而看到所需的属性。

On the other hand, when you're logging $scope.getCurrentUser._id , that's the result of a property lookup on the promise object - and it prints the current value of the property, which is undefined . 另一方面,当您登录$scope.getCurrentUser._id ,这是在promise对象上进行属性查找的结果-并显示该属性的当前值,该值undefined

A note about clean code: your scope variable is called getCurrentUser, which makes one think it's a getter function, but it is infact the return value of a getter function. 关于干净代码的注释:您的作用域变量称为getCurrentUser,这使人认为它是一个getter函数,但实际上它是getter函数的返回值。 This is confusing. 这很混乱。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM