0

I have an Angular Frontend with Spring-Boot Backend integration. For testing purposes i want to get database entries in the Console.log. It works that I get the specific entries, but the Output is only [object Object] instead of the real database-entry.

This are my service-methods (just a part), where I got a Get-Method for getting all my entries and jsut for a specific entry (which is marked with an ID):

getVorgaenge(): Observable<any> {
    return this.http.get(this.getDataURL);
  }

  getVorgaengeID(id: number): Observable<any> {
    return this.http.get(`${this.getDataURL}/${id}`);
  }

My component.ts looks like this:

constructor(private http: HttpClient, private vorgangService : VorgangService) { }

  vorgaenge: any;

  ngOnInit() {
    this.getAllVorgaenge();
  }


  getAllVorgaenge() {
    this.vorgangService.getVorgaenge().subscribe(
      data => {
        this.vorgaenge = data;
        console.log("Alle Einträge:" + this.vorgaenge);

        data = Math.max.apply(0, this.vorgaenge.map(function(v) {return v.id}));
        console.log("Höchster Eintrag: " + data);

        this.vorgangService.getVorgaengeID(data).subscribe(newdata => {
          this.vorgaenge = newdata;
        })
        console.log("Eintrag mit höchster ID: " + this.vorgangService.getVorgaengeID(data));  

      })
  }

I want to get the last entry of the database (i've done this with the Math.max-function. It works fine, but the output in the console is only "Eintrag mit höchster ID: [object Object]"

I need the exact entry of the last entry (of which i get the ID with Math.max). It should be working, but the output needs to be converted to the real json-database-entry.

1
  • [object Object] because this.vorgangService.getVorgaengeID(data) return observable Commented Jun 25, 2019 at 16:40

2 Answers 2

1

You're concatenating an object with string so it gives you [Object object] as a string representation.

You should either log it with comma or use JSON.stringify.

var data = { a: "1", b: "2" };

console.log("My data is " + data); // Wrong
console.log("My data is ", data); // Right
console.log("My data is " + JSON.stringify(data)); // Also not bad
Sign up to request clarification or add additional context in comments.

Comments

0

You can use console.table if your data is in proper JSON format. ex:

console.table(data);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.