"I am looking for a way to receive a response from a loopback in Angular7

Currently, I am utilizing angular7 with loopback. While I can successfully retrieve data, I am unsure how to receive error messages and response statuses. It would be helpful for me to understand what my response code is at the time of the request.

Output of Data

[
 {
"name": "karthik",
"lastname": "Selvakumar",
"course": "BE",
"year": "IV",
"id": 2
 }
]

Answer №1

To retrieve the response code from the HttpResponse, you can use the following code snippet:

import { Component, OnInit } from '@angular/core';

import { HttpClient } from  "@angular/common/http";
@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: [ './app.component.css' ]
})
export class AppComponent implements OnInit {
  status;

  constructor(private http: HttpClient) {}


  ngOnInit() {
    this.http.get('https://jsonplaceholder.typicode.com/posts?page=0', {observe: 'response'})
    .toPromise().then((response) => {
      this.status = response.status;
    })
  }
}

Check out the working example on StackBlitz

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Tips on including text within PrimeNG speeddial option buttons?

Instead of icons, I would like to have text displayed in the options of my speed dial component. This is what I am aiming for Thank you in advance ...

Ways to reveal heavily nested components when the Angular change detector fails to detect them

A particular component called Grid has been implemented with the following template: <div *ngFor="let row of item.rows"> <div *ngFor="let col of row.cols"> <div *ngFor="let grid of col.items"> < ...

Next.js does not recognize the _app file

After including the _app.tsx file in my project to enclose it within a next-auth SessionProvider, I noticed that my project is not recognizing the _app.tsx file. Even after adding a div with an orange background in the _app.tsx file, it does not reflect in ...

Generating TypeScript Type Definitions for dynamic usage

In my client server application, I use REST calls for communication. To avoid using the wrong types by mistake, I have defined all RestCalls in a common file (excerpt): type def<TConnection extends Connections> = // Authentication ...

I'm trying to figure out how to redirect only the empty path using the new router in Angular 2. Can anyone

Environment: Upgraded to RC4 with brand new router Here is the current configuration of my router.. export const routes: RouterConfig = [ {path: 'search-documents', component: SearchDocumentsComponent}, { path: '', pathMat ...

I want to create a feature where a video will automatically play when a user clicks on a specific item in a list using Angular

Currently, I'm working on a project that involves displaying a list of videos and allowing users to play them in their browser upon clicking. The technology stack being used is Angular 2. Below is the HTML code snippet for achieving this functionalit ...

Utilizing TypeScript to Retrieve the Parameter Types of a Method within a Composition Class

Greetings to the TS community! Let's delve into a fascinating problem. Envision having a composition interface structured like this: type IWorker = { serviceTask: IServiceTask, serviceSomethingElse: IServiceColorPicker } type IServiceTask = { ...

The function in Angular 2 is invoked a total of four times

Take a look at this example. This example is derived from an Angular quick start sample, where the template invokes a function. import { Component } from '@angular/core'; @Component({ selector: 'my-app', template: `<h1>Hell ...

Angular route unable to detect Passport User Session

The navigation bar in the header features buttons for Register, Login, and Become a Seller. Upon user login, it displays logout and view profile buttons. This functionality is operational on all routes except one, causing a client-side error. user not def ...

Create Angular file structures effortlessly using a tool similar to Rails scaffold

Is there a code generator in Angular similar to RoR's rails scaffold? I am looking to run a specific command and receive the following files, such as: *.component.html *.component.sass *.component.ts *.module.ts. ...

Angular9 displays a variable as undefined

As a newcomer to Angular 9, I have been encountering some issues lately. Specifically, while using Ionic 5 with Angular 9, I am facing an error where a variable appears as undefined when I try to console.log it. Please bear with me as I explain the situati ...

"Exploring the process of extracting data from a JSON URL using NextJS, TypeScript, and presenting the

After tirelessly searching for a solution, I found myself unable to reach a concrete conclusion. I was able to import { useRouter } from next/router and successfully parse a local JSON file, but my goal is to parse a JSON object from a URL. One example of ...

Firestore rule rejecting a request that was meant to be approved

Recently, I came across some TypeScript React code that fetches a firestore collection using react-firebase-hooks. Here's the snippet: const [membersSnapshot, loading, error] = useCollectionData( query( collection(db, USERS_COLLECTION).withConve ...

Tips for retrieving and organizing multiple values from a form using php

I am struggling to understand how to properly create this type of form. Unsure if my current approach is correct. The form I have created generates a delivery based on a list of products in the SQL table, each with a unique product ID and quantity. https ...

Dissimilarities in behavior between Angular 2 AOT errors

While working on my angular 2 project with angular-cli, I am facing an issue. Locally, when I build it for production using ng build --prod --aot, there are no problems. However, when the project is built on the server, I encounter the following errors: . ...

Find the highest values from a table that has been joined twice

In my database, I have three tables structured as follows: +-----------------+--------+ | ident (PrimKey) | ident2 | +-----------------+--------+ | 123 | 333 | | 321 | 334 | | 213 | 335 | | 1234 | ...

Using Angular to declare a variable for reuse within nested HTML elements

Exploring the realm of angular development has sparked my interest, however, I found myself at a roadblock while reading through the documentation. The issue lies in figuring out how to declare a variable that can be reused effectively within nested HTML e ...

Error in Angular production build due to Clean CSS failure

Encountering the following error during the code build process, any solutions? > ng build --prod --no-aot --base-href 92% chunk asset optimizationD:\myproject\node_modules\@angular\cli\node_modules\clean-css\lib&bsol ...

Error encountered in NextJS API: "TypeError: res.status is not a function"

Background In my development environment, I am using NextJS v11.1.1-canary.11, React v17.0.2, and Typescript v4.3.5. To set up a basic API endpoint following the guidelines from NextJS Typescript documentation, I created a file named login.tsx in the pag ...

What is the best way to retain all checkbox selections from a form upon submission?

I have a batch of checkboxes that correspond to the profiles I intend to store in the database. HTML: <tr *ngFor="let profile of profiles | async"> <input type='checkbox' name="profiles" value='{{profile.id}}' ng-model=&apo ...