0

I am trying to get all the data from mongodb but it shows [object] [object] how do I get array data to angular from mongodb? Here is my code:

import { Component, OnInit } from '@angular/core';
import {HttpClient} from "@angular/common/http";

import {Model} from "../model/model";

@Component({
  selector: 'app-take-quiz',
  templateUrl: './take-quiz.component.html',
  styleUrls: ['./take-quiz.component.css']
})
export class TakeQuizComponent implements OnInit {
  data:Model[]=[];

  constructor(private http:HttpClient) { }

  ngOnInit(){
    this.quizAll();
  }

  quizAll(){
    this.http.get<any>('https://localhost:8080/quizzes/').subscribe(response=>
    {
      console.log(response);
      // @ts-ignore
      this.data=response;

     });

  }
}

model.ts

export interface Model {
  title:string;
  description:string;
}

html

<div>
  <p>your data {{data}}</p>
</div>

Here is the screenshot of console snapshot of console

when I try

<div>
      <p>your data {{data.title}}</p>
</div>

It shows following error error image

1 Answer 1

1

Use the JSON Pipe in order to display the whole data as string . Detailed explanation in https://www.concretepage.com/angular-2/angular-2-json-pipe-example and data used in the html is an array not an object. In order to show all title loop through data as given below

<span *ngFor="let val of data">{{val.title}}</span>

Sign up to request clarification or add additional context in comments.

1 Comment

I went through the documentation but I'm not able to get the exact value it always throws object when I try to get value from html file.

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.