วิธีใช้ observable ข้อมูลจากเว็บเบราว์เซอร์ภายนอกรูปแบบ api ใน nestjs?

0

คำถาม

ข้อความด่วนทันใจพยายามที่จะใช้จากเว็บเบราว์เซอร์ภายนอกรูปแบบ api ใน nestjs กับ axios.

@Injectable()
export class PIntegration {
  constructor(private httpService: HttpService) { }
  API = process.env.API || 'http://localhost:3000';
  header = { headers: { 'Content-Type': 'application/json' } };

  async getPrestacionById(id: string): Promise<Observable<IPrestacion>>{
   
    return this.httpService.get(`${this.API}/prestacion/${id}`, this.header).pipe(map((res) => res.data));
  }
}

และของบริการเรียนดูเหมือนนี้:

@Injectable()
export class ProductService{
    constructor(private pIntegration: PIntegration){}
    async producto(id: string) {
        const infoPrestacion = await  this.pIntegration.getPrestacionById(id);
        console.log({ infoPrestacion })
        if (infoPrestacion)
        {
             if (infoPrestacion.detalles) {
                console.log(infoPrestacion.detalles)
                console.log("tiene detalles")
            }
        }
        return infoPrestacion;
    }
}

แต่ถ้าฉันคอนโซล.ปูมบันทึกค่า"infoPrestacion"นี่คือผลลัพธ์ที่ได้:

{
  infoPrestacion: Observable {
    source: Observable { _subscribe: [Function (anonymous)] },
    operator: [Function (anonymous)]
  }
}

และมัน doesnt องไปสองตั้งแต่มันไม่ได้แก้ไขเลยนะ มันเป็นไปได้ที่จะรอสำหรับผลจนกระทั่งมันกแก้ไขเรียบร้อยแล้(ฉันไม่มี config สำหรับ HttpModule)? การกลับมาจริงๆจะได้รับวัตถุงตัวเอง"infoPrestacion"แต่ฉันต้องการที่จะทำงานกับค่าและไม่กลับมาที่วัตถุ.

axios nestjs nestjs-config
2021-11-23 15:25:15
1

คำตอบที่ดีที่สุด

0

ฉันแก้ปัญหาของฉันกับเรื่องนี้ฉันหวังว่านี่อาจจะเหมาะสมได้ตามต้องการ.

ถ้าคุณเอาของคุณ observable เป็นสัญญาว่ามันมีสองทางออกที่อาจเหมาะสำหรับคุณ

ในชั้นเรียนคุณใช้เว็บเบราว์เซอร์ภายนอกรูปแบบ api:

เพิ่ม lastValueFrom ซึ่ง converts เป็น observable ต้องสัญญาโดย subscribing ที่ observable รอให้มันสมบูรณ์และการแก้ไขที่กลับคืนสัญญากับสุดท้ายค่าจากสังเกตุการณ์สายข้อมูลสื่อนี้

firstValueFrom จะเป็นยังเป็นทางออก,มันตรงข้ามกับของ lastValuefrom ที่ธาตุก่อนที่สัญญาของคุณคือไขกระจ่างแล้ว

@Injectable()

export class PIntegration {
  constructor(private httpService: HttpService) { }
  API = process.env.API || 'http://localhost:3000';
  header = { headers: { 'Content-Type': 'application/json' } };

  async getPrestacionById(id: string): Promise<Observable<IPrestacion>>{
   
    return lastValueFrom(this.httpService.get(`${this.API}/prestacion/${id}`, this.header).pipe(map((res) => res.data)));
  }
}
2021-11-26 13:34:41

ในภาษาอื่นๆ

หน้านี้อยู่ในภาษาอื่นๆ

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
中文
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................

ดังอยู่ในนี้หมวดหมู่

ดังคำถามอยู่ในนี้หมวดหมู่