Having trouble inserting the current time into Firebase Firestore

Currently, I am looking to use the current time as an input in Firebase Firestore (timestamp). Initially, when using the code snippet below:

today: number = Date.now();

everything appeared to function correctly. However, the time was only updated once, specifically when the page was downloaded. To address this, I implemented a time update function:

updatedTime(): void {
  setInterval(() => {
  this.today = new Date();
}, 1000);}

and then triggered it within the ngOnInit() function:

ngOnInit() {
   this.updatedTime();
}

This approach successfully displayed the updated time in the browser. Unfortunately, it did not reflect when attempting to input _today into Firebase Firestore.

How can this issue be resolved?

Additionally, is it more advisable to utilize the Date object or opt for the timestamp provided by Firebase APIs?

Answer №1

To obtain a timestamp object in Firestore, make sure to include a Date object in the document being created or updated, like new Date(). Avoid using numerical values, such as those generated by Date.now(), as it will only display the number itself. This rule applies across all language bindings with Firestore, each requiring the use of their respective native Date type.

Answer №2

It seems like you are asking about how to incorporate the current timestamp when adding or updating data. One way to achieve this is by using the built-in function

firebase.firestore.FieldValue.serverTimestamp()
.

For example, you can use the following code snippet...

const firebase = require('firebase');
const firestore = require('firebase/firestore');

// ...

let docRef = await this.db.collection('accounts').doc(accountId);
docRef.update({
  name,
  lastModified: firebase.firestore.FieldValue.serverTimestamp()
});

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 retrieve the value of an object based on its key?

I'm working on a function that returns another function, and I need some guidance: fn: <T extends Object>(key: keyof T) => (value: ???) => void I want the type of ??? to be determined by the type of instanceOfT[key]. For instance, in the ...

Issue encountered in v-on event handler: "authentication is not a valid function" - Vue.js

I am currently incorporating Firebase into a VueJS project. My API credentials are stored in a file named fb.js import firebase from 'firebase' var firebaseConfig = { apiKey: "*****", authDomain: "*****", proje ...

What is the process for deploying a Lambda function using Terraform that has been generated with CDKTF

Currently, I am following a tutorial by hashicorp found at this link. The guide suggests using s3 for lambda deployment packages. // in the process of creating Lambda executable const asset = new TerraformAsset(this, "lambda-asset", { ...

Exploring the Impact of 2 HostBindings on Class Generation from Inputs in Angular 4

I'm struggling with the code in my component, which looks like this: @Component({ selector: "home", templateUrl: "./home.html" }) export class Home { constructor() {} @HostBinding("class") @Input() public type: string = "alert" @HostBindi ...

Leverage async-await in conjunction with subscription

I am struggling with a tangled mess of code known as 'callback hell'. Can someone please guide me on how to make use of async-await to simplify the debugging process and tidy up this situation? this.ws.call('vm.image_path', ['Ran ...

Angular Module without any components

The Angular modules documentation provides an example of a module that includes a template, a component, and a provider. This module is then imported into the root module and instantiated by using the element defined by the component like this: <app-co ...

Java Spark API using basic authentication will issue a 401 error response when accessed from an Angular application

My Java Spark API is set up for basic authentication. When I make a call to the URL from Postman with basic auth, it returns a JSON response. However, when I make the same call from an Angular 4 application, I get the error message: "Response for preflight ...

Callbacks are never fired in SQL Server because of the tedious connection

I have successfully connected to a SQL Server instance hosted in Azure through DBeaver and can browse all the data. After installing tedious with the following commands: npm install tedious npm install @types/tedious This is the exact code I am using: im ...

Angular HTTP client fails to communicate with Spring controller

Encountered a peculiar issue in my Angular application where the HttpClient fails to communicate effectively with the Spring Controller. Despite configuring proper endpoints and methods in the Spring Controller, the Angular service using HttpClient doesn&a ...

While validating in my Angular application, I encountered an error stating that no index signature with a parameter of type 'string' was found on type 'AbstractControl[]'

While trying to validate my Angular application, I encountered the following error: src/app/register/register.component.ts:45:39 - error TS7053: Element implicitly has an 'any' type because expression of type 'string' can't be used ...

substitute one item with a different item

I am facing an issue with updating the address object within an organization object. I receive values from a form that I want to use to update the address object. However, when I try to change the address object in the organization using Object.assign, i ...

What is the best way to save the output of an asynchronous function into a class attribute?

Currently, I am attempting to retrieve HTML content from a webpage by utilizing a class equipped with a single asynchronous method. This process involves Typescript 3.4.3 and request-promise 4.2.4. import * as rp from 'request-promise'; class H ...

You cannot use Angular 5 to send a post request with a token that has been retrieved

Hello, I'm facing an issue with making a post request in Angular 5. The token I retrieve seems correct as it works fine when tested with Postman. Can someone provide me with a hint or suggestion on what could be going wrong? AuthService.ts getProfi ...

Obtaining information from an API using Angular

I am currently working on extracting data from various API's and I am encountering some difficulties. The initial part is functioning correctly, with the code provided below : ngOnInit(): void { this.http.get('http://.../api/getData?table=ge ...

Is it possible to import SVG files and inline them in Angular?

Behold, an SVG Circle: <svg viewBox="0 0 104 104"> <circle cx="52" cy="52" r="50" stroke="#003EFF" stroke-width="4" fill="#00FF98" /> </svg> The Angular Project imports it in this manner: import circle from './circle.svg'; ...

Guide to connecting data from the backend to the frontend in the select option feature using Angular 9

I have a backend system where I store a number representing a selected object, which I am trying to display in a select option using Angular. Currently, the select option only displays a list of items that I have predefined in my TypeScript code using enu ...

Encountering an Issue while Attempting to Deploy Angular 6 on Her

I recently pushed my Angular 6 application to Heroku. Although the deployment was successful, the page is not loading properly. An error message on the page reads: An error occurred in the application and your page could not be served. If you are the ap ...

Optimal method for retrieving data from a JSON object using the object's ID with a map

Can you teach me how to locate a json object in JavaScript? Here is a sample Json: { "Employees" : [ { "userId":"rirani", "jobTitleName":"Developer", "preferredFullName":"Romin Irani", "employeeCode":"E1", "region":"CA", "phoneNumber":"408-1234567", " ...

Find a string that matches an element in a list

I currently have a list structured like this let array = [ { url: 'url1'}, { url: 'url2/test', children: [{url: 'url2/test/test'}, {url: 'url2/test2/test'}], { url: 'url3', children: [{url: & ...

What is the best way to transform an array containing double sets of brackets into a single set of brackets?

Is there a way to change the format of this list [[" ", " ", " ", " ", " ", " ", " ", " ", " ", " "]] to look like [" ", " ", " &qu ...