F
F
Felino2018-06-11 13:27:11
Angular
Felino, 2018-06-11 13:27:11

Writing to jSON DB?

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);
  }

}

Tell me why the record in the database does not work!
Swears at
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)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
0
0xD34F, 2018-06-11
@Felino

And who will initialize the data property? Or do you think it will assign something other than undefined to itself in self-service mode?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question