Issue with Angular 6 custom pipe causing NaN when utilizing service-sourced data

I am working on a custom pipe that converts a given number into days and hours. The HTML code for this pipe is as follows:

<div *ngFor="let unit of units">
   <p>Remaining Life</p>
   <h2>{{ unit.daysRemaining | timeRemaining }}</h2>
</div>

Initially, the unit object was located within the component, and the pipe successfully converted the number. You can view it in action on this StackBlitz link.

However, I decided to load the data using a service, and that's when the values started showing as undefined (I have set it up to display as 0 in the pipe). Take a look at the outcome in this modified StackBlitz.

What could be causing this discrepancy between the two scenarios? Why does the value turn into undefined when retrieving data from a service?

Answer №1

Issue:

You have created an object array in your service, where each object contains a property called hoursRemaining. However, in your app.component.html, you are trying to access a property called daysRemaining which is not defined in the objects.

Resolution:

To resolve this issue, make sure to access the correct property hoursRemaining instead of daysRemaining in the app.component.html.

<hello name="{{ name }}"></hello>
<div>
    <div>Custom Pipe:</div>
    <ul>
        <li *ngFor="let unit of units">{{ unit.hoursRemaining | daysRemaining }}</li>
    </ul>
</div>

For a working example, check out this link.

Answer №2

Your current setup shows:

<li *ngFor="let unit of units">{{ unit.daysRemaining | daysRemaining }}</li>

however, it should display:

<li *ngFor="let unit of units">{{ unit.hoursRemaining | daysRemaining }}</li>

This correction is needed in the second stackblitz within app.component.html

Answer №3

Resolution:

To resolve the issue, simply replace daysRemaining with hoursRemaining in the template.

Suggestion

If you use

<li *ngFor="let unit of units">{{ unit | json }}</li>
, it can be an effective method for debugging as it allows you to see the data passed to your pipe.

Answer №4

To ensure smooth functioning of your code, the following adjustments should be made:

<li *ngFor="let timeUnit of units">{{ timeUnit.hoursRemaining | daysRemaining }}</li>

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

Arabic text spread across multiple lines within the queryString

When attempting to call an ASP.NET page from JavaScript with a query string containing Arabic text, I encounter an error when the code is live online but it runs smoothly on the local server. The issue seems to arise when the Arabic text spans multiple l ...

What is the most effective way to send a list of objects to a Controller

https://i.stack.imgur.com/oyi5v.png This form is for billing purposes. You can add the item name, quantity, and price multiple times to calculate the total amount. Once you click on submit, all the included items along with other parameters like bill nu ...

Substitute all items identified by a particular tag with a component

Is it possible to replace elements with React? I am interested in replacing all elements with a specific tag with an input field when an event like clicking an 'edit' button occurs. I have experience doing this with jQuery, but I would prefer us ...

Encountering a CORS problem when attempting to upload pictures to Cloudinary from a React application

I'm having trouble with CORS while attempting to upload images to Cloudinary from my React app hosted at http://localhost:5173. I've configured CORS on Cloudinary, added the necessary headers to my fetch request, and double-checked my Cloudinary ...

Tips for isolating the month values from the res.body.results array of objects with the help of JS Array map()

Looking to adjust the custom code that I have which aims to extract months from a string using regex. It seems like I'm almost there but not quite hitting the mark. When checking the console log, I am getting "undefined" values for key/value pairs and ...

What is the correct way to effectively utilize ng-bind (one-way binding) with form inputs?

section, I have observed that changing the value of input2 manually and then altering the values of input1 results in input 2 not being updated any further. This raises the question: does this mean that the one-way binding in the form inputs will be disru ...

Jest throws an error: require function is not defined

I've been struggling with an issue in Angular for the past 3 days. I'm using [email protected] and [email protected]. I even tried downgrading and testing with LTS versions of node and npm, but I keep encountering the same error. Here ...

Querying Firebase to find documents that do not have a specific requested field present in all

My current project is using firebase. I am currently working on retrieving documents from firebase, but I have encountered a specific issue. The problem lies in the fact that I have older documents without a "hidden" field and newer documents with this fie ...

Submit HTML and JavaScript information to a PHP document

I have a form with a single file input and a submit button. The action attribute of the form is set to 'upload.php' where my PHP script resides for renaming and saving the uploaded file to a specific folder. Additionally, I require some JavaScrip ...

What is the most effective method in Vue.js for transitioning elements without the need to use v-for and v-if at the

Utilizing v-for to generate multiple <p> elements, each one animating in within a Vue <transition> component if the v-if condition is met. Below is the code snippet: <transition name="fade"> <p v-for="(quote, idx) in game.quot ...

How can I use jQuery to switch classes or activate a click event on the most recently clicked element?

Within my <ul></ul>, there are 4 <li> elements. <ul class="wcarchive-terms-list"> <li class="wcarchive-term wcarchive-term-parent woof_term_224"> <label class="wcarchive-term-label"> <span cla ...

I am still receiving an empty dropdown value despite implementing ng-selected

I am having issues with using ng-selected to retrieve the selected value from a dropdown. Instead of displaying the selected value, it appears blank. Here is the code snippet I have tried: <div> <select id="user_org" ng-model="selectedorg.all ...

The issue of "URLSearchParams not being defined" arises during test execution using jest

Is there a way to utilize URLSearchParams while testing with Jest? Every time I attempt to do it, the following error occurs: ReferenceError: URLSearchParams is not defined ...

What is the best way to extract the value from a React date picker every time the dates are modified?

Currently, I am utilizing a React library known as react-date-picker and my goal is to retrieve the date every time it is modified. Below is the default code given by the library for selecting the date from a dropdown: import React, { Component } from "r ...

The Javascript code is functioning properly in Chrome, however, it is experiencing compatibility issues in other

Recently, I put together a simple web page using React and Express. One of the modules features a basic form with text input fields, an email input field, and a submit button that is supposed to send an email containing data from the input fields to me. To ...

Is there a way to access data from a previous useState hook in a subsequent useState hook?

For my current project, I am working on developing a small marketplace website that integrates with stripe Checkout. The tutorial I was following did not cover using products from an API, which is where I encountered some difficulties. The table in the AP ...

Leveraging Google maps to find nearby stores

I recently created a store locator but hit a roadblock when I discovered that Google Maps does not allow you to iframe the entire page. Is there a workaround for this issue to display the map? Or is there an alternative method that doesn't involve ifr ...

How to set a new default environment in Angular CLI for running ng serve

Is there a way to make Angular CLI use a different environment file instead of the default dev when running ng serve? Update I should have clarified my question better. I am aware that we can specify the environment file using the --env switch, but I am ...

Trouble with Date.parse() function in JavaScript on Mozilla Firefox browser

I'm encountering an issue with parsing the dateTime value "01-01-2013 12:00:00 AM" in Mozilla Firefox. I have successfully parsed it using Date.parse("01-01-2013 12:00:00 AM") in Google Chrome and IE browsers, but Firefox seems to be giving me trouble ...

What is the best way to use command line arguments within a Node.js script?

As a newcomer to the world of programming, I find myself struggling to make progress despite putting in significant effort. Although it may seem like a trivial question, my lack of progress is becoming frustrating. Currently, I am working on a node.js scr ...