What is the best way to showcase data from my Firebase database collection?

This HTML Code is designed to display elements retrieved from Firebase.

<div class= "sentMessages" *ngFor= "let item of snapshot">
  {{item.timestamp.toDate() | date:"medium"}}
  {{item.name}}
  {{item.message}}
</div>

Contained within app.component.ts, this code aims to fetch a collection from the database.

export class AppComponent {
  title = 'ChatApp';
  name: string;
  color: string = "#127bdc";
  message: string = "";
  messages: any;
  snapshot: any


  constructor (private db: AngularFirestore){
    this.name = "";
    this.messages =db.collection('messages');
  }
  async addMessage(){
 
      const res = await this.db.collection('messages').add({
        name : this.name,
        color : this.color,
        message : this.message,
        timestamp: new Date()  });

        this.snapshot = await this.messages.get();

  }
}

Although addMessage properly saves user input to the Firebase database upon hitting enter, it fails to display the current database contents.

Answer №1

learn more about updating data with Angular change detection
solution :

  async sendMessage(){

  const response = await this.db.collection('messages').add({
    senderName : this.senderName,
    messageColor : this.messageColor,
    messageContent : this.messageContent,
    dateTime: new Date()  });

    const allMessages = await this.messages.get();
    this.allMessagesSnapshot= [...allMessages]
}

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 proper way to define the type when passing a function as a component prop, with or without parameters?

import { dehydrate, HydrationBoundary } from '@tanstack/react-query'; import getQueryClient from '@/lib/react-query/getQueryClient'; export async function RQBoundary<T>({ children, queryKey, fn, }: { children: React.Reac ...

Angular 6 and Typescript: How to Map Objects in Arrays within Arrays

We currently have two arrays named speisekarte (consisting of 10 objects) and essensplan (containing 8 objects). const speisekarte = [ { id: 11, name: 'Kabeljaufilet', price: 3.55, type: 'with fish' }, { id: 12, name: 'Spaghet ...

How can I dynamically change the prefix in an angular router?

In my Angular app, I have two main routes: dashboard and login: www.example.com/dashboard www.example.com/auth/login These routes are defined as follows: const routes = [ { path: 'dashboard', component: DashboardComponent }, { path: 'auth ...

Using tsc with skipLibCheck flag will still perform checks on the node_modules directory

When I run the CLI command npx tsc --noEmit --skipLibCheck, I still encounter errors: node_modules/@types/node/util.d.ts:1631:41 - error TS1005: '(' expected. 1631 keys(): IterableIterator<string>; ...

I am unable to locate the module '@schematics/angular/utility/config'

I attempted to execute the command below in order to add ngx-bootstrap to my project: ng add ngx-bootstrap Unfortunately, I encountered an error message. The full CLI output is displayed below: i Using package manager: npm √ Found compatible package ver ...

Using ngFor to connect input with the Algolia Places feature

I have implemented an Algolia Places input within an ngFor loop using Angular8. The issue I am facing is that the (change) event only works properly after typing in the input for the second time. While this is functional, it's not exactly the behavior ...

Position the mat-icon button in the top right corner for the close button

I'm struggling with positioning my close icon button at the top right corner of my dialog box. Here is my app-iconbutton component: <button mat-icon-button class="iconbutton" [ngStyle]="{ 'background-color': back ...

issues arising from the kendo theme design within the angular framework

In my Angular project, I am utilizing the Kendo user interface, but I am struggling to figure out how to adjust the size and apply different themes to all the elements. For instance, I would like to have a small button with a danger color. While it is pos ...

Angular 2: Creating a Reusable Object for Uniform JSON Structures

I am facing an issue with JSON data as I have 3 tables in my database named "dictionary" with the same structure but different column names: {"id":1,"test_1":"test"},{"id":2,"test_1":"lalala"} - first JSON {"id":1,"test_2":"****"},{"id":2,"test_2":"afe ...

Managing simultaneous asynchronous updates to the local state

There is a scenario where a series of asynchronous calls are made that read from a local state S, perform certain computations based on its current value, and return an updated value of the local state S'. All these operations occur at runtime, with ...

Two-way data binding between TypeScript and HTML FormGroups

Attempting to create my first Reactive form in Angular Here is the code : import { Component, OnInit } from '@angular/core'; import { FormGroup, FormControl } from "@angular/forms"; @Component({ selector: 'app-username-password ...

Unable to assign a value to an element obtained from the Axios response in React

I'm having trouble mapping an array, it doesn't seem to be working. You can find the code here : https://codepen.io/ilaan16/pen/eYRKgOm setUtils(result.data); if (!utils) { console.log("ERROR_UTILS", result); } else if ...

Setting an array of objects using TypeScript in the useState hook: A step-by-step guide

const response = { results: { items: [ { name: 'item1', }, { name: 'item2', }, { name: 'item3', }, { ...

NgFor is designed to bind only to Iterables like Arrays

After exploring other questions related to the same error, I realized that my approach for retrieving data is unique. I am trying to fetch data from an API and display it on the page using Angular. The http request will return an array of projects. Below ...

Dealing with the Firebase Storage XMLHttpRequest undefined issue in Nuxt JS

I require assistance with my web app, which involves uploading an image to firebase storage and then displaying that image in a thumbnail. The error message I am encountering states that this.xhr_ = new XMLHTTPREQUEST is not defined Unfortunately, I do n ...

Transferring information from a child to parent component within Angular using the <router-outlet> component

I currently have the following code in my app.component.html file: <div> <app-login (un)="doSth($event)"></app-login> </div> <router-outlet (un)="doSth($event)"></router-outlet> And in my app.com ...

Is it possible to integrate the Firestore npm library into my Express application?

Recently, I created my own library to act as a nosql database on my node.js web server in place of mongodb. I came across this interesting quote: Applications that use Google's Server SDKs should not be used in end-user environments, such as on pho ...

Steps for managing files in Ionic Native: creating, reading, and writing them

Struggling to find proper examples for file operations like creating, reading, and writing text or logs into a file? I've done a lot of research but haven't stumbled upon any suitable solutions. The examples provided in this link seem helpful, ho ...

Sorry, I cannot complete this task as it involves rewriting copyrighted content

I recently implemented the useRef hook in my scroll function, specifying HTMLDivElement as the type. However, I encountered an issue where I received the error message "Property 'clientHeight, scrollHeight, scrollTop' does not exist on type &apos ...

Identify the specific directive selector utilized within the template by examining multiple directive selectors

My directive has two selectors, dirA and dirNotA, and I need to determine which one was used within the directive itself. I want to avoid creating multiple directives or using parameters in order to achieve this. Ideally, I would like to have a single dire ...