角度6“字符串”不可分配给“数字”类型

时间:2018-09-09 15:33:09

标签: node.js angular mongodb mongoose angular6

我在解决该错误方面遇到了麻烦。

this.ticketService.updateTicket(this.edit_ticket)

this.edit_ticket带红色下划线,表示类型'string'不可分配给'Number'类型。

在edit_ticket模型中,“ timeInHours”和“ price”是数字,但是我不知道如何以json格式声明它们。

我尝试过

timeInHours: Number;

但是它给了我另一个错误:类型'string'不能分配给类型'NumberConstructor'。

如果我正确理解了我从HTML中获取的值,则是字符串,甚至是数字输入。我该如何更改?

编辑工作正常,但IDE中的错误令人讨厌。

代码在下面。

edit_ticket = {
    _id: '',
    timeInHours: '',
    location: '',
    price: '',
    vehRegistration: '',
    email: '',
    mobile: ''
}

editTicket(){
    this.edit_ticket._id = this.id;
    this.edit_ticket.timeInHours = (<HTMLInputElement>document.getElementById('timeInput')).value;
    this.edit_ticket.location = (<HTMLInputElement>document.getElementById('locInput')).value;
    this.edit_ticket.price = (<HTMLInputElement>document.getElementById('priceInput')).value;
    this.edit_ticket.vehRegistration = (<HTMLInputElement>document.getElementById('regInput')).value;
    this.edit_ticket.email = (<HTMLInputElement>document.getElementById('emailInput')).value;
    this.edit_ticket.mobile = (<HTMLInputElement>document.getElementById('mobInput')).value;


    this.ticketService.updateTicket(this.edit_ticket)
        .subscribe((data)=>{ 
        console.log("success");
});

TicketService代码:

import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { Ticket } from './ticket';
import { Observable } from 'rxjs';

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

  private apiUrl: string = "http://localhost:3000/tickets/";

  constructor(private http: HttpClient) { }

  getTickets(): Observable<Ticket[]>{
    return this.http.get<Ticket[]>(this.apiUrl);
  }

  getTicketsByReg(reg): Observable<Ticket[]>{
    console.log(this.apiUrl + "reg/" + reg);
    return this.http.get<Ticket[]>(this.apiUrl + "reg/" + reg);
  }

  getTicketById(id): Observable<Ticket>{
    console.log(this.apiUrl + id);
    return this.http.get<Ticket>(this.apiUrl + id);
  }

  deleteTicket(id): Observable<Ticket>{
    console.log(this.apiUrl + id);
    return this.http.delete<Ticket>(this.apiUrl + id);
  }

  updateTicket(ticket: Ticket): Observable<any>{
    const headers = new HttpHeaders()
   .append('Content-Type' , 'application/json');

    return this.http.put(this.apiUrl + ticket._id, ticket);
  }
}

0 个答案:

没有答案