【问题标题】:How to get specific Object to the angular如何获得特定对象的角度
【发布时间】:2021-03-22 16:50:55
【问题描述】:

我使用 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 谢谢你

【问题讨论】:

  • 添加this.customerService.getCustomer(id)的定义
  • 我添加了 noh..查看我的代码
  • 不,您没有添加包含getCustomer 定义的服务文件。
  • 构造函数(私有toastr:ToastrService,私有customerService:CustomerService,私有路由器:路由器,私有对话:MatDialog,私有路由:ActivatedRoute,){ this.loadAll(); }
  • 请添加您服务中的代码。

标签: node.js angular mongodb typescript


【解决方案1】:

你最好 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'));

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-11-03
    • 2019-07-27
    • 1970-01-01
    • 2013-12-27
    • 2018-06-08
    • 1970-01-01
    • 1970-01-01
    • 2021-09-05
    相关资源
    最近更新 更多