角返回未定义

时间:2019-05-23 06:37:51

标签: angular typescript

我有API可以获取有关数据库中特定餐厅的信息,但是我必须通过POST请求来获取它。餐馆登录后,我成功从auth.service和另一个API中获取了restaurantID,但是当我尝试在控制台中登录餐馆时,却得到了undefined。一致,我没有在此处显示API的权限。代码:

restaurant.service.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);
  }

}

informacije.component.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;

  }
}


3 个答案:

答案 0 :(得分: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()方法,所以数据不可用

答案 1 :(得分: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);
    }); 
}

答案 2 :(得分:0)

尝试一下:

informacije.component.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;

  }
}