Kindth
Kindth

Reputation: 337

Error doesn't show data from database (spring boot + angular 8)

I want to display data on frontend from database using RestController but it doesnt show nothing and no errors so I need a help to resolve this issue any suggestion may help me and thanks

ServiceClass:

@Injectable({
    providedIn: 'root'
  })
export class EmployeService{


    private URL_RESOURCES:String = 'http://localhost:8080/api/employe';
    constructor(private http: HttpClient){

    }

    public getEmp():Observable<Employe[]>{
      return this.http.get<Employe[]>(`${this.URL_RESOURCES}`);
    }

ComponentService:

export class EmployeesComponent implements OnInit {
  _employesArray:Employe[]=[];
  constructor(private http:HttpClient, private employeservice:EmployeService) {

   }

  ngOnInit() {
    this.dataSource.sort = this.sort;
    this.dataSource.paginator = this.paginator;
    this.reloadData();
  }

  reloadData() {
    this.employeservice.getEmp().subscribe(
      data=>{
        this._employesArray=data;
      },
      err=>{
        alert("An error has occured")
      }
    );
}
}

class:

export class Employe {

    id: number;
    name: string;
    prenom: string;
}

AppComponent.html:

<tbody>
   <tr *ngFor="let employee of _employesArray ">
   <td>{{employee.id}}</td>
   <td>{{employee.name}}</td>
   <td>{{employee.prenom}}</td>

    </tr>
</tbody>

CollaborateurController:

@RestController
@CrossOrigin(origins="http://localhost:4200")
@RequestMapping("/api")
public class CollaborateurController {
    @Autowired
    private CollaborateurRepository collaborateurRepository;

    @GetMapping(value="/employe")
    public List<Collaborateur> getEmp() {
        return (List<Collaborateur>) collaborateurRepository.findAll();
    }

ICollaborateur:

public interface ICollaborateur {


    List<Collaborateur> getEmp();

}

Upvotes: 0

Views: 698

Answers (2)

Kindth
Kindth

Reputation: 337

I add this to the main and the issue is fixed

@SpringBootApplication(exclude= {
    org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration.class
})

Upvotes: 0

Vishal Khare
Vishal Khare

Reputation: 41

Go to network tab and see if there is any backend call. If there is no backend call, you need to debug your service file on why http call is not getting through. If the backend call is happening, there can be 2 possibilities. Either there is an error with status code anything other than 200. This will be comparatively easier to fix once we know what the error is. Or if response status code is 200 then you are getting the data from backend. I see you have an interface. Why do you have an interface if you are not implementing it anywhere?

Upvotes: 1

Related Questions