1

我有用于获取数据库中一家特定餐厅信息的 API,但我必须通过 POST 请求来获取它。当餐厅登录时,我成功地restaurantID从 auth.service 和另一个 API 获取,但是当我尝试在控制台中登录餐厅时,我得到undefined. 统一我无权在此处显示 API。编码:

餐厅服务.ts

import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';
import { Restaurant } from '../models/Restaurant';
import { LoggedRestaurant } from '../models/LoggedRestaurant';

import { AuthService } from './auth.service'

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

  private restaurantUrl = 'https://dm.dnevnimeni.com/dmnew/podacirestorana.php';

  public restaurant: Restaurant;
  public loggedRestaurant: LoggedRestaurant
  public restaurantID = this.authService.currRestaurant[0].id

  constructor(private http: HttpClient, private authService: AuthService) { }

  getRestaurant(ID): Observable<LoggedRestaurant> {

    console.log('ID je' + this.restaurantID);

    return this.http.post<LoggedRestaurant>(this.restaurantUrl, ID);
  }

}

信息组件.ts

import { Component, OnInit } from '@angular/core';
import { AuthService } from '../services/auth.service';
import { RestaurantService } from '../services/restaurant.service';
import { Restaurant } from '../models/Restaurant';
import { LoggedRestaurant } from '../models/LoggedRestaurant';
import { Observable } from 'rxjs';

@Component({
  selector: 'app-informacije',
  templateUrl: './informacije.component.html',
  styleUrls: ['./informacije.component.scss']
})
export class InformacijeComponent implements OnInit {
  restaurant: Restaurant;
  loggedRestaurant: LoggedRestaurant;
  restaurantID = this.authService.currRestaurant[0].id;;

  constructor(private restaurantService: RestaurantService, private authService: AuthService ) { }

  getRestaurant() {
     this.restaurantService.getRestaurant().subscribe(data  => {
      this.loggedRestaurant = data;
    });

  }

  ngOnInit() {

    this.getRestaurant();

    this.restaurant = this.authService.currRestaurant[0];
    console.log(this.restaurant)
    console.log(this.loggedRestaurant)

    this.restaurantID = this.restaurant.id;
    console.log(this.restaurantID)
    this.restaurantService.restaurantID =this.restaurantID;

  }
}


4

3 回答 3

1

更新您的代码应该是这样的

由于您只需要获取数据,因此您不必使用 post

所以你可以改变这个

return this.http.post<LoggedRestaurant>(this.restaurantUrl, this.restaurantID);

对此

return this.http.get<LoggedRestaurant>(`${this.restaurantUrl}/${this.restaurantID}`);

并添加 ngOnInit

ngOnInit() {
this.restaurantService.getRestaurant().subscribe(data  => {
   this.loggedRestaurant = data;
   // do something else
});

因为在 ngOnInit 生命周期挂钩中未调用您的 getRestaurant() 方法,所以数据不可用

于 2019-05-23T06:44:51.650 回答
0

您的代码有一些问题。首先,您从未真正调用过该getRestaurant()函数,因此永远不会请求服务调用。

其次,您正在处理异步代码并且不能期望服务调用在console.log(this.loggedRestaurant)运行之前完成。

我的建议是您将函数更改为返回Observable<LoggedRestaurant>并订阅它。

getRestaurant(): Observable<LoggedRestaurant> {
     this.restaurantService.getRestaurant().subscribe(data  => {
      this.loggedRestaurant = data;
    });
}

然后您可以将其用作

ngOnInit() {
    this.getRestaurant().subscribe(loggedRestaurant => {
        console.log(loggedRestaurant);
    }); 
}
于 2019-05-23T06:47:14.767 回答
0

尝试这个:

信息组件.ts

import { Component, OnInit } from '@angular/core';
import { AuthService } from '../services/auth.service';
import { RestaurantService } from '../services/restaurant.service';
import { Restaurant } from '../models/Restaurant';
import { LoggedRestaurant } from '../models/LoggedRestaurant';
import { Observable } from 'rxjs';

@Component({
  selector: 'app-informacije',
  templateUrl: './informacije.component.html',
  styleUrls: ['./informacije.component.scss']
})
export class InformacijeComponent implements OnInit {
  restaurant: Restaurant;
  loggedRestaurant: LoggedRestaurant;
  restaurantID;

  constructor(private restaurantService: RestaurantService, private authService: AuthService ) { }

  getRestaurant() {
     this.restaurantService.getRestaurant().subscribe(data  => {
      this.loggedRestaurant = data;
    });

  }

  ngOnInit() {
    this.getRestaurant(); // add this line
    this.restaurant = this.authService.currRestaurant[0];
    console.log(this.restaurant)
    console.log(this.loggedRestaurant)

    this.restaurantID = this.restaurant.id;
    console.log(this.restaurantID)
    this.restaurantService.restaurantID =this.restaurantID;

  }
}
于 2019-05-23T06:47:17.807 回答