如何获得该对象的价值?

时间:2019-08-02 17:58:33

标签: json angular mongodb typescript

我正在使用 Angular 8从无线电输入中获取数据。当我将该数据发送到 Node 然后发送到 Mongodb 数据库时,它没有注册,并且在db集合中显示为:

"__v" : 0

Angular 发送的数据输出为:{ type: administrator },我要在数据库中注册的只是单词adminstrator Node 控制台显示错误,因为该对象在期望字符串值时传递给db。

Angular模板是这样的:

<br>
<div class="w3-panel w3-border w3-margin" style="width: 500px; height: 550px">

        <label class="w3-text-blue"><h2>New User</h2></label>
        <label class="w3-text-blue"><b>Name and Last Name</b></label><br>
           <input type="text" class="w3-input w3-border" [(ngModel)]="name" ><br>

        <label class="w3-text-blue"><b>User</b></label><br>
           <input type="text" class="w3-input w3-border" [(ngModel)]="user"><br>

        <label class="w3-text-blue"><b>Password</b></label><br>
           <input type="password" class="w3-input w3-border" [(ngModel)]="password"><br>

        <label class="w3-text-blue"><b>Confirm Password</b></label><br>
           <input type="password" class="w3-input w3-border" [(ngModel)]="confirm"><br>

        <label class="w3-text-blue"><b>Type of User</b></label><br>

        <form #myForm="ngForm" (submit)="sendRadio(myForm.value)" >
            <input id="administrator" type="radio" name="type" class="w3-radio" value="administrator" ngModel>
               <label for="administrador">Administrador</label> <br>
            <input id="normal" type="radio" name="type" class="w3-radio" value="normal" ngModel>
               <label for="normal">Moderador</label>
     <br><br>
        <button type="submit" (click)="createUser(user, password, name)" class="w3-button w3-round-xxlarge w3-green">Create</button>
        <button class="w3-button w3-round-xxlarge w3-red">Clean Fields</button>
</form>

</div>

组件的打字稿文件:

import { Component, OnInit } from '@angular/core';
import { PersonService } from '../_services/person.service';

@Component({
  selector: 'app-create-user',
  templateUrl: './create-user.component.html',
  styleUrls: ['./create-user.component.css']
})
export class CreateUserComponent implements OnInit {

  user: any;
  password: any;
  confirm: any;
  name: any;
  type: string;

  constructor(private ps: PersonService) { }

  ngOnInit() {
  }

  sendRadio(value: any) {
    this.type = value;
    console.log(this.type);  // <--- this outputs { type: administrator }
  }

  createUser(userU, userPassword, userName) {
    if (this.password === this.confirm) {
       if (this.password.length > 0 && this.confirm.length > 0) { 
          this.ps.createUser(userU, userPassword, userName, this.type);
          } else {
            console.log('Password has to match and be completed');
            }
    }else {
        console.log('Password does not match');   

    }
  }
}

发送数据的服务(PersonService):

import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';

@Injectable({
  providedIn: 'root'
})
export class PersonService {

  uri = 'http://localhost:3000';

  constructor(private http: HttpClient) { }

  createUser(userU, userPassword, userName, userType: string) {
    const obj = {
      user: userU,
      password: userPassword,
      name: userName,
      type: userType
    };
    this.http.post(`${this.uri}/createUser`, obj)
    .subscribe(res => console.log('User has been created'));
  }

1 个答案:

答案 0 :(得分:1)

只需传入

console.log(this.type.type);

因为您需要type的密钥的 value 。这会打印出“管理员”,您可以将其传递到后端节点。