Quantcast
Channel: Recent Questions - Stack Overflow
Viewing all articles
Browse latest Browse all 12201

How to request data from own Express back-end server using HttpClient in Angular/TypeScript?

$
0
0

I'm encountering the problem that every time I'm requesting data with the HTTP GET method I cannot store the data in any way.

My API service:

export class ApiService implements OnInit{  private baseUrl = 'http://localhost:3000/api/portfolio/'  constructor(    private http: HttpClient  ) {  }  getAllProjects(): Observable<any[]> {    return this.http.get<any[]>(this.baseUrl +'getProjects')  }  ngOnInit() {    this.getAllProjects()  }

The component that receives the data:

export class GetProjectsComponent implements OnInit {  response: any  constructor(    private projectService: ApiService  ) {}  ngOnInit(): void {    this.getAllProjects()  }  getAllProjects(): void {    this.projectService.getAllProjects().subscribe(      (response: any) => {        this.response = response // I'M NOT ABLE TO JUST STORE THE DATA I JUST RECIEVED.      }    )  }

A part of backend server.js file:

app.get('/api/portfolio/getProjects', async (req, res) => {    try {        const database = client.db(DATABASE_NAME);        const collection = database.collection("projects");        const cursor = collection.find({});        const results = await cursor.toArray();        res.send(results);    } catch (error) {        console.error("Error getting projects:", error);        res.sendStatus(500).json({ error: "Failed to get projects" });    }})

I tried to store the data in the user's sessionStorage. Did not work.


Viewing all articles
Browse latest Browse all 12201

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>