简体   繁体   English

我无法访问两个对象中的 id

[英]I am not able to access the id inside both objects

I am new to angular, I am not able to post the values of the id inside Position and Department, for some reason, it is not accessing the id我是 angular 的新手,我无法在 Position 和部门内发布 id 的值,由于某种原因,它没有访问 id

addEmployee.model.ts addEmployee.model.ts

export class AddEmployee {

  Position: {
    id: string;
  };
  Department: {
    id: string;
  };
}

AddEmployeeService添加员工服务

DefineEmployee(addEmployee: AddEmployee) {
  const body: AddEmployee = {

    Position: {
      id: addEmployee.Position.id
    },

    Department: {
      id: addEmployee.Department.id
    }
  }
  return this.http.post('url', body);
}

AddEmployee.component.ts AddEmployee.component.ts

onSubmit(form: NgForm) {

  return this.restService.DefineEmployee(form.value).subscribe((res: any) => {
    this.resetForm(form);
    console.log(res);
  }, error => {
    this.resetForm(form);
  })
}

addEmployee.component.html addEmployee.component.html

<div class="form-group">

  //AddEmployee.Position.id comes up with undefined element 'id'

  <select [(ngModel)]="AddEmployee.Position.id" name="Position" Position="ngModel" class="form- 
             control customized-dropdown">
    <option [value]="item.id" *ngFor="let item of positions">{{item.name_FL}}</option>
  </select>
</div>

as the error is coming from the service AddEmployeeService , I believe the call to the service is not passing the parameters your function is expecting.由于错误来自服务AddEmployeeService ,我相信对服务的调用没有传递您的 function 期望的参数。

When you call this.restService.DefineEmployee(form.value) , I believe the value property does not have a Position or Department property.当您调用this.restService.DefineEmployee(form.value)时,我相信value属性没有PositionDepartment属性。

you can test that by adding a debugger as indicated:您可以通过添加调试器来测试它:

DefineEmployee(addEmployee: AddEmployee) {
  console.log(addEmployee);
  debugger;
  const body: AddEmployee = {

    Position: {
      id: addEmployee.Position.id
    },
    ...

that should help you fix the function这应该可以帮助您修复 function

UPDATE更新

The Op posted the contents of the addEmployee varible as: Op 将addEmployee变量的内容发布为:

{
 "Position":"c8a06de6-58e1-439a6fc-08d7553139ac",
 "Department":"552ccb91-02f8-476b-adbc-08d7553136bf"
}

it seems the ids of Positions and Departments do not have an 'id' property in the "addEmployee" variable;似乎职位和部门的 id 在“addEmployee”变量中没有“id”属性;

so to fix the issue, just remove the 'ids' form the addEmployee in the service所以要解决这个问题,只需从服务中的addEmployee中删除“ids”

DefineEmployee(addEmployee: AddEmployee) {
  const body: AddEmployee = {

    Position: {
      id: addEmployee.Position // <<< here
    },

    Department: {
      id: addEmployee.Department  // <<< here
    }
  }
  return this.http.post('url', body);
}

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

相关问题 我无法在我的网站上访问我的问题的正确ID - I am not able to access correct id of my ques in my website 我无法在angularjs和ionic中访问控制器内部的if语句 - I am not able to access the if statement inside the controller in angularjs and ionic 无法访问 Javascript 中的 JSON 内的嵌套对象 - Not able to access nested objects inside a JSON in Javascript 我无法在表格内填写选择 - I am not able to fill a select inside a form 我是否能够将 json 对象字符串值的 2 个部分解构为 2 个变量? - Am I able to destructure 2 parts of a json objects string value into 2 variables? 我想遍历DOM,但无法访问数据 - I want to traverse across the DOM , I am not able to access the data 我可以在选项标签内动态添加img标签吗? - Am I able to add an img tag inside the option tag dynamically? 使用Firebug,我可以看到HTML <Div> 父内部的部分,当我使用Selenium来获取基于 <div id> ,我看不到孩子 - With Firebug i am able to see HTML<Div>section inside the parent, when i use Selenium to fetch the html source based <div id>, i cannot see the child 为什么我不能使用JavaScript访问CSS属性? - Why am I not able to access CSS properties with JavaScript? 为什么我无法在setTimeout中访问事件值? - Why I am not able to access event value in setTimeout?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM