首页 > 解决方案 > 如何获得特定对象的角度

问题描述

我使用 angular 作为前端,而 node.js 使用 mongodb 作为后端。我想通过给定的_id获取特定的客户对象。我创建了 node.js 方法并能够使用邮递员发送请求并能够获得结果。但我无法将输出发送到前端。你能给出一个以表格或段落输出结果的解决方案吗?

Node.js 代码:

    const getCustomer = (req, resp) => {
    let {id} = req.params;
    CustomerSchema.findById(id).then(result =>{
        resp.status(200).json(result);
      }).catch(error =>{
        resp.status(500).json(result);
      })   
   };

角码:

    private setCustomerToForm() {
    this.route.params.subscribe(params => {
      const id = params['id'];
      if (!id){
        return;
      }
       this.customerService.getCustomer(id).subscribe(customer => {
       this.customer = customer;
       console.log(this.customer)
      },error => this.onWarning('Error'));


    });
  }

客户 DTO 包含 CustomerName、CustomerNic 谢谢

标签: node.jsangularmongodbtypescript

解决方案


你最好 console.log({data}) 先看看你从后端得到了什么。尝试这个:

this.customerService.getCustomer(id).subscribe(data => {
   console.log({data});
   const { customer } = data;
   this.customer = customer;
   console.log(this.customer);
},error => this.onWarning('Error'));

推荐阅读