如何使用get从httpclient URL中推出数组中的数据?

发布于 2025-02-09 13:44:31 字数 1518 浏览 3 评论 0原文

我仍然是编程的新手,如果问题可能很愚蠢,我深表歉意。

但是我想知道如何在数组中保存“ get(url)”数据。 此数据在数组中,我想执行数组的长度

一旦 检索数组的长度,我将制作一种随机值以显示随机面的方法

import { Observable, Subject, Subscription } from 'rxjs';
import { FaceSnap } from './../models/face-snap.model';
import { Injectable } from "@angular/core";
import { HttpClient } from '@angular/common/http';
@Injectable({
  providedIn: 'root' //on renregistre ce service à la racine de l'application
})

export class FaceSnapService {


  // newFaceSnap$ = new Observable<FaceSnap[]>();

  constructor(private http:HttpClient){

  }

getAllFaceSnaps(): Observable<FaceSnap[]>{
  // return this.faceSnaps;
  return this.http.get<FaceSnap[]>('http://localhost:3000/facesnaps');
}

getFaceSnapById(faceSnapId: number): Observable<FaceSnap>{
  // const faceSnap = this.faceSnaps.find(faceSnap => faceSnap.id===faceSnapId)
  return this.http.get<FaceSnap>(`http://localhost:3000/facesnaps/${faceSnapId}`);
}


showOneFaceSnap(){
  const faceTab = [];

  this.http.get<FaceSnap[]>('http://localhost:3000/facesnaps').subscribe(facesnaps=> 
  facesnaps.forEach(data=> faceTab.push(data)) );


  console.log("array ",faceTab, "array length ", faceTab.length );

}
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.7.5/angular.min.js"></script>

I'm still a newbie in programming and I apologize if the question may be stupid.

But i want to know how i can save data from a "get(url)" in an array. Once this data is in an array, I want to do the length of the array but when i do a "console.log to check the values and the length with "array.length" it always show "0".

In fact,after retrieving the length of the array, I would make a method to retrieve a random value to display a random faceSnap.

My problem is in the function : showOneFaceSnap()

import { Observable, Subject, Subscription } from 'rxjs';
import { FaceSnap } from './../models/face-snap.model';
import { Injectable } from "@angular/core";
import { HttpClient } from '@angular/common/http';
@Injectable({
  providedIn: 'root' //on renregistre ce service à la racine de l'application
})

export class FaceSnapService {


  // newFaceSnap$ = new Observable<FaceSnap[]>();

  constructor(private http:HttpClient){

  }

getAllFaceSnaps(): Observable<FaceSnap[]>{
  // return this.faceSnaps;
  return this.http.get<FaceSnap[]>('http://localhost:3000/facesnaps');
}

getFaceSnapById(faceSnapId: number): Observable<FaceSnap>{
  // const faceSnap = this.faceSnaps.find(faceSnap => faceSnap.id===faceSnapId)
  return this.http.get<FaceSnap>(`http://localhost:3000/facesnaps/${faceSnapId}`);
}


showOneFaceSnap(){
  const faceTab = [];

  this.http.get<FaceSnap[]>('http://localhost:3000/facesnaps').subscribe(facesnaps=> 
  facesnaps.forEach(data=> faceTab.push(data)) );


  console.log("array ",faceTab, "array length ", faceTab.length );

}
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.7.5/angular.min.js"></script>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

乖乖公主 2025-02-16 13:44:31

AS rsd 提到,数据可在订阅中可用

let faceTab = [];

this.http.get<FaceSnap[]>('http://localhost:3000/facesnaps').subscribe(data=> 
 {
   faceTab = data;
   console.log(faceTab);
 });

As RSD mentioned, data is available inside the subscription

let faceTab = [];

this.http.get<FaceSnap[]>('http://localhost:3000/facesnaps').subscribe(data=> 
 {
   faceTab = data;
   console.log(faceTab);
 });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文