import { Component, OnInit } from '@angular/core';
import {FormControl, FormGroup, Validators} from '@angular/forms';
import {TicketsService} from '../../share/service/tickets.service';
import {Tickets} from '../../share/models/tickets.model';
@Component({
selector: 'anv-list-buy',
templateUrl: './list-buy.component.html',
styleUrls: ['./list-buy.component.css']
})
export class ListBuyComponent implements OnInit {
form:FormGroup;
data:Tickets;
constructor(private ticketsService:TicketsService){}
ngOnInit() {
this.form=new FormGroup({
'payeer':new FormControl(null,[Validators.required,Validators.maxLength(7)]),
'email':new FormControl(null,[Validators.required,Validators.email]),
//'ticket':new FormControl(null,[Validators.required,Validators.max(7)])
});
}
onBuy(){
this.data.payeer=this.form.value.payeer;
this.data.email=this.form.value.email;
console.log(this.data);
this.ticketsService.setTickets(this.data);
}
}
export class Tickets {
constructor(
public email: string,
public payeer: string,
public win: boolean = false,
public id?: number
) {
}
}
import {HttpClient} from '@angular/common/http';
import {Observable} from 'rxjs';
import {Tickets} from '../models/tickets.model';
import {Injectable} from '@angular/core';
@Injectable()
export class TicketsService {
constructor(private http:HttpClient){}
getWinTickets():Observable<Tickets[]>{
return this.http.get<Tickets[]>('http://localhost:3000/tickets');
}
setTickets(ticket:Tickets):Observable<Tickets[]>{
console.log(ticket)
return this.http.put<Tickets[]>('http://localhost:3000/tickets',ticket);
}
}
Подскажите почему не работает запись в БД!
Ругается на
ERROR TypeError: Cannot set property 'payeer' of undefined
at ListBuyComponent.push../src/app/list-navigation/list-buy/list-buy.component.ts.ListBuyComponent.onBuy (list-buy.component.ts:26)
at Object.eval [as handleEvent] (ListBuyComponent.html:2)
at handleEvent (core.js:11107)
at callWithDebugContext (core.js:12204)
at Object.debugHandleEvent [as handleEvent] (core.js:11907)
at dispatchEvent (core.js:8561)
at core.js:10044
at SafeSubscriber.schedulerFn [as _next] (core.js:3724)
at SafeSubscriber.push../node_modules/rxjs/_esm5/internal/Subscriber.js.SafeSubscriber.__tryOrUnsub (Subscriber.js:253)
at SafeSubscriber.push../node_modules/rxjs/_esm5/internal/Subscriber.js.SafeSubscriber.next (Subscriber.js:191)