Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/31.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
Node.js Nodejs角轮廓_Node.js_Angular_Profile - Fatal编程技术网

Node.js Nodejs角轮廓

Node.js Nodejs角轮廓,node.js,angular,profile,Node.js,Angular,Profile,我的Angular应用程序有一个问题,我想在Angular 5中的profile root中发送用户的数据,但它不起作用 我使用nodejs和angular 5。 错误如下: 错误:找不到不同的支持对象“[object]” 类型为“对象”。NgFor仅支持绑定到Iterables,例如 数组 我的根节点配置文件: router.get('/dashboard', passport.authenticate('jwt', { session: false }), function(req

我的Angular应用程序有一个问题,我想在Angular 5中的profile root中发送用户的数据,但它不起作用

我使用nodejs和angular 5。

错误如下:

错误:找不到不同的支持对象“[object]” 类型为“对象”。NgFor仅支持绑定到Iterables,例如 数组

我的根节点配置文件:

router.get('/dashboard', passport.authenticate('jwt', { session: false }), 
    function(req, res) {
       User.findById({_id : req.user.id}, function(err, user) {
       res.json(user);
    });
});
角度剖面图.service.ts

  constructor(private http: HttpClient) {}

  getProfile() {
     const uri = 'http://localhost:4000/api/dashboard';
     return this.http.get(uri).map(res => {
        return res;
     });
  }
角度剖面.component.ts

export class ProfileComponent implements OnInit {

   profiles: any;
   constructor( private prof: ProfileService) {}

   ngOnInit() { this.getProfile(); }

   getProfile() {
      this.prof.getProfile().subscribe(res => {
        this.profiles = res;
      });
   }
}
Html视图

<div *ngFor="let profile of profiles">
   {{ profile.email }}
</div>

{{profile.email}

在您的后端,您的服务将返回一个作为对象的配置文件

findById -> object
/dashboard -> object
getProfile(http get) -> object
 export class ProfileComponent implements OnInit {

   profile: object;
   constructor( private prof: ProfileService) {}

   ngOnInit() { this.getProfile(); }

   getProfile() {
      this.prof.getProfile().subscribe(res => {
        this.profile = res;
      });
   }
}
因此,您需要将变量显示为对象

findById -> object
/dashboard -> object
getProfile(http get) -> object
 export class ProfileComponent implements OnInit {

   profile: object;
   constructor( private prof: ProfileService) {}

   ngOnInit() { this.getProfile(); }

   getProfile() {
      this.prof.getProfile().subscribe(res => {
        this.profile = res;
      });
   }
}
html


ngFor在数组上迭代。要在对象上迭代,请尝试以下操作

*ngFor=“let profilekey of Object.key(profiles)” 而内心的表达,


{{profiles[profilekey].email}

我找到了一种方法,它很有效

在我的服务中,我添加

  getProfile() {
        const uri = 'http://localhost:4000/api/dashboard';
        return this.http.get<any>(uri).map(res => {
        return res;
     });
  }
我的看法

{{ profile.email}}

一个好的开始应该是在getProfile()中进行日志记录,以查看API调用返回的内容。此外,如果您要返回一个可观察或承诺,您可以在模板中直接将其与异步管道绑定。最后,我建议您使用类型脚本类型检查,这样您就不会依赖匿名对象。如果我使用您的方法,我可以在视图中显示json,但我有一个小错误ProfileComponent.html:11错误类型错误:无法读取未定义的ProfileComponent的属性“email”。html:11错误类型错误:无法读取的属性“email”undefined@ValentinValentyn您的json看起来是什么样子?{“\u id”:“5af563f17f434b31907712a3”,“nom”:“FRF”,“电子邮件”:fr@fr.fr“,”密码“$2a$10$KBLWFjNC3dvIFl9W2wQJeOxgBDdzWNLWUKoIJ64cWBjVchM/Fq.Fy”,“\uu v”:0}”