我正在使用 https://github.com/DaftMonk/generator-angular-fullstack .
我有 2 个模式:
- 图表
- 标题:字符串
- 数据集:数组
- _creator:登录用户ID
- 用户
- 姓名
- 电子邮件
- _id
当我 console.log 当前用户返回
$scope.getCurrentUser = Auth.getCurrentUser();
console.log($scope.getCurrentUser);
但是当我尝试控制台记录 _id 时:
$scope.getCurrentUser = Auth.getCurrentUser();
console.log($scope.getCurrentUser._id);
返回
undefined
有人可以解释我做错了什么吗?
最佳答案
chrome 控制台中的对象只有在首次打开时才会被评估。
这意味着当您 console.log 一个像 Auth.getCurrentUser()
的返回值这样的对象时,控制台会显示对它的引用——这在日志调用时包含一个 promise 对象,但它很可能在您在控制台上打开它时得到解决,因此您会看到您正在寻找的属性。
另一方面,当您记录 $scope.getCurrentUser._id
时,这是对 promise 对象进行属性查找的结果 - 它会打印属性的当前值,这未定义
。
关于简洁代码的注意事项:您的作用域变量称为 getCurrentUser,这让人认为它是一个 getter 函数,但实际上它是 getter 函数的返回值。这令人困惑。
关于javascript - Angular-fullstack 获取当前用户 ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28966023/