F
F
Felino2018-06-13 11:54:59
Angular
Felino, 2018-06-13 11:54:59

Angular Http how?

Hello!
Guys tell me this question!
I made a service

getWinTickets():Observable<Tickets[]>{
    return this.http.get<Tickets[]>('http://localhost:3001/win');
  }

  getTickets():Observable<Tickets[]>{
    return this.http.get<Tickets[]>('http://localhost:3000/tickets');
  }

  setTickets(ticket:Tickets):Observable<Tickets[]>{
    return this.http.post<Tickets[]>('http://localhost:3000/tickets',ticket);
  }

  setWinTickets(ticket:Tickets[]):Observable<Tickets[]>{
    return this.http.post<Tickets[]>('http://localhost:3001/win',ticket);
  }

in the service it turns out like this:
getWinTickets - returns from the database
setWinTickets - writes to the database
getTickets - returns from the database
setWinTickets - writes to the database
Tell me how to implement this functionality correctly
: it needs to be taken every third element and written to win. How to implement it correctly?
So far I've implemented it like this
onWinNumber() {
    this.winnumberService.getTickets().subscribe(ticket => {
      this.ticket = ticket.filter(c => c.id % 3 == 0);
    });
    this.winnumberService.setWinTickets(this.ticket).subscribe();

    console.log(this.ticket);
  }

But it turns out that when the function is triggered, it returns just an empty array for the first time, and the subsequent ones are from those that I received from the database!
Is there a way to get data from the database based on specific criteria? If possible, give an example please, otherwise I didn’t even find it on the Internet (
Thanks

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Eremin, 2018-06-13
@Felino

There may be a problem with synchronism
Try to do setWinTickets exactly when getTickets will work

onWinNumber() {
    this.winnumberService.getTickets().subscribe(ticket => {
      this.ticket = ticket.filter(c => c.id % 3 == 0);
      this.winnumberService.setWinTickets(this.ticket).subscribe();
      console.log(this.ticket);
    });
  }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question