How can I retrieve an empty object from the database using Angular 5?

Hey there! I'm facing a little issue that I need help solving. I asked a question before when things weren't clear, but now they are (you can find the previous question here). I'll share my code and explain what I want to achieve shortly.

Here's the data in my database:

posts 
     --- 1 
          --- publisher: "Erick" 
          --- content:   "Something is written here"
          --- comments 
                      --- 1
                           --- comment: "Yes"
                           --- commentator: "Patrick"  
                      --- 2 
                           --- comment: "I don't think so"
                           --- commentator: "Sarah"
     --- 2 
          --- publisher: "Patrick" 
          --- content:   "News here.."
          --- comments 
                      --- 1
                           --- comment: "Yes"
                           --- commentator: "Ines"  

I retrieve the data in my app.component.ts:

export class AppComponent implements OnInit {

    pubs: Observable<any[]>;
    constructor(private db: AngularFireDatabase){
    }
    ngOnInit(){
       this.pubs = this.getData('/posts');
    }

    getData(path): Observable<any[]>{
       return this.db.list(path).valueChanges()
    }

}

Below is my HTML code:

<div *nfFor="let post of pubs | async ">
    <p>Publisher: {{post.publisher}}</p>
    <p>Content: {{post.content}}</p>
    <div>{{post.comments}}</div>
    <ul> 
       <li *ngFor="let cmt of post.comments" >{{cmt?.comment}}</li>
    </ul>
</div>

However, you can see that my first object is empty, causing issues as shown below:

Publisher: Erick
Content: Something is written here 

,[object Object],[object Object]

-                
- Yes
- I don't think so



Publisher: Patrick
Content: News here 

,[object Object]

- 
- Yes

This problem is reflected in the image linked https://i.sstatic.net/RvigT.png.

I would greatly appreciate any assistance with this. Thank you!

Answer â„–1

That's because when you use {{post.comments}}, it automatically calls the Object.toString() method which results in **[object Object]** being displayed. You can resolve this by using the json pipe to stringify your object before displaying it.

Give this code a try:

<div *ngFor="let post of pubs | async">
    <p>Publisher: {{post.publisher}}</p>
    <p>Content: {{post.content}}</p>
    <div>{{post.comments | json}}</div>
    <ul> 
       <li *ngFor="let cmt of post.comments" >{{cmt?.comment}}</li>
    </ul>
</div>

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

What is the best way to develop a unique animation for every v-card?

Is there a way to customize each animation so that it is specific to the selected v-card? Right now, when one card is clicked, all of them play the same animation. data: () => ({ show: true, images: [ {url:require('@/assets/london. ...

Optimizing the particle rendering speed for HTML5 <canvas> elements

Currently conducting an experiment to enhance the maximum particle count before frame-rates begin to decrease in HTML5 Canvas. Utilizing requestAnimationFrame and employing drawImage from a canvas as it appears to be the most efficient method for image re ...

Implementing state management in ReactJS

I'm a newcomer to ReactJS and I've encountered an issue with the setState function in my code. The expectation was for the value to increment by 1 every second, but that isn't happening. import React from 'react'; import ReactDOM ...

Tips for importing modules for unit tests using QUnit

Lately, I've been working on implementing unit tests for some of my modular ES6 code. The project structure I have looks something like this: project └───src | └───js | cumsum.js | index.js <--- main file └─┠...

Exploring the wonders of math in Angular with ng-repeat

Exploring the realm of mathematics within an angular expression, let's consider a scenario where a user can either have credit on the site or receive a percentage discount. Below is the code snippet in question: <div ng-repeat="item in NewArrivals ...

What kind of mischief can be wreaked by a malicious individual using JavaScript?

My mind has been consumed by thoughts about the safety of my projects, especially when it comes to password recovery. On the password recovery page, users must fill out a form with valid data and complete a recaptcha test for security. To enhance user ex ...

Setting a consistent theme or style for all HTML/React tags using a selector inside a specific component

Here's a simplified example of what I'm trying to do: I'm using Material UI Styles for styling my components. I want to style all the <Link> tags in my component. For instance: const useStyles = makeStyles(theme => ({ menuLink: ...

Vue.js transition-group does not apply the *-move class

As I dive into learning Vue, I find myself wondering if I may have overlooked something fundamental or stumbled upon a bug. Despite multiple readings of the documentation at https://v2.vuejs.org/v2/guide/transitions.html#List-Move-Transitions, I still can& ...

Setting up $routeProvider in Express 4 using 'app.config' method in Angular JS: A guide

I'm currently facing an issue where app.config is mentioned as the only place where $routeProvider can be invoked. However, with Express 4 removing app.config, what is the alternative method to call it? Previously : var app = angular.module(&apos ...

Effortless AJAX Submission and MySQL Data Refresh

I've hit a roadblock and can't seem to figure things out. I'm getting rid of my current code because it's not working well across different browsers, particularly when it comes to ajax submission. Could someone please provide me with a ...

Are there alternative methods for retrieving data in Vue Hacker News besides using prefetching?

I am currently developing a Vue single page application with server side rendering using Vue SSR. As per the official documentation, data in components will be prefetched server-side and stored in a Vuex store. This process seems quite intricate. Hence, ...

Automatically switch to dark mode at specified times: A simple guide

Here is the current method for toggling themes: let themeToggler = document.getElementById('theme-toggler'); themeToggler.onclick = () => { themeToggler.classList.toggle('fa-sun'); if (themeToggler.classList.contains('f ...

jquery toggleClass not retaining previous click event

I came across a show/hide function that seemed promising for my issue, which I found here (credit to the original author). Inspired by this, I created a demonstration on JSFiddle (https://jsfiddle.net/q7sfeju9/). However, when attempting to show rows, it d ...

Error encountered at / - undefined local variable or method `parameters' for main:Object (Executing Stripe Charge with Stripe.js)

Encountering an error with the code below while attempting to create a Stripe charge using Stripe.js. Below is my web.rb file: require 'json' require 'sinatra' require 'sinatra/reloader' require 'stripe' get &a ...

Issue with socket malfunctioning when integrated with express

I’m encountering an issue with the socket in my program. While I can easily broadcast from any part of the program using "io" connection, I face limitations when trying to use "socket" for broadcasting unless it is within the same connection as "io." I a ...

Tips on uploading multiple images to Firebase Storage and retrieving multiple downloadURLs

Currently, we are in the process of developing a straightforward e-commerce application that requires uploading multiple product images. By utilizing Vuejs and Vue-Croppa, our objective is to upload these images to Firebase storage, retrieve their download ...

Capture data from clipboard as it is being pasted into Handsontable

Issue Description and Troubleshooting: In a nutshell, I am seeking a method to manage data copied from the clipboard to a handsontable. I came across a suggestion on how to fetch this information using jQuery in a stackoverflow post: $("#haras_excel_li ...

Vue 3 throws an error stating: "An uncaught DOMException occurred because the string contains an invalid character."

Exploring the capabilities of vue.js on a basic website. The setup consists of a single index.html file and all JavaScript is housed in an index.js file. The website is a work in progress, but there are no blockers preventing the JavaScript functionality ...

Creating dynamic and engaging animations for components using ReactCSSTransitionGroup

I'm currently attempting to add animation to a modal that appears when a button is clicked using ReactCSSTransitionGroup. The modal is showing up on button click, however, there is no transition effect. My render method is set up like this: render() ...

What is preventing Facebook from merging its CSS/JS files together?

I wonder about the reasoning behind Facebook developers' decision not to consolidate their scripts and stylesheets into single files, opting instead to load them on demand through their CDN. Considering the complexity of Facebook as an application, I ...