When working with the Typescrypt Bitcoin Currency Exchange in Angular 4, make sure that the right-hand side of any arithmetic operation is either of type 'any', 'number', or an enum type

I am currently in the process of developing a currency exchange application using Angular 4, but I have encountered an issue when trying to update a property value. Allow me to elaborate:

My app includes a service that fetches the current Bitcoin price in Chilean Pesos (CLP) from an API Service and stores this information in an Interface. My goal is to automatically convert a user-input CLP amount into BTC. Initially, I tested this functionality with a hardcoded random number.

Component.ts:

exchangeRate : 715000;
targetAmount = 1;
baseAmount = this.exchangeRate;

update(baseAmount) {
    this.targetAmount = parseFloat(baseAmount) / this.exchangeRate;
}

and here is the corresponding DOM:

<div class="form-group">
        <label for="company">Monto en CLP</label>
        <input type="number" class="form-control" id="company" placeholder="Ingrese el monto en CLP" (input)="update($event.target.value)">
</div>

This initial setup worked perfectly, but then I decided to dynamically retrieve the exchange rate from the API Service by adding the following lines of code:

Component.ts

minask(price: SurbtcMarketView): number {
        return price.min_ask;
    }

Interface.ts

export class SurbtcMarket {
  public ticker: SurbtcMarketView;
}

export class SurbtcMarketView {
  public last_price : number;
  public min_ask : number;
  public max_bid : number;
  public volume : number;
  public price_variation_24h : number;
  public price_variation_7d : number;
}

Now, instead of using a static value like 715000 for the exchange rate, I want to utilize the minask function as follows:

exchangeRate : this.minask;
targetAmount = 1;
baseAmount = this.exchangeRate;

update(baseAmount) {
    this.targetAmount = parseFloat(baseAmount) / this.exchangeRate;
}

However, when attempting to use `this.exchangeRate` in the calculation, I encounter the following error:

The right-hand side of an arithmetic operation must be of type 'any', 'number', or an enum type

Can anyone provide guidance on how to address this issue? Thank you for your assistance!

EDIT:

Below is the service responsible for fetching the data used in setting `this.exchangeRate`:

service.ts

import { Injectable } from '@angular/core';
import { Http, Headers, Response } from '@angular/http';
import 'rxjs/add/operator/toPromise';
import {Observable} from "rxjs";
import 'rxjs/Rx';
import 'rxjs/add/operator/catch';
import { SurbtcMarket } from './surbtcmarket'


@Injectable()
export class SurbtcService {

  constructor(private http: Http) { }

  public getPricess() :Observable<SurbtcMarket> {
    return this.http.get('https://www.surbtc.com/api/v2/markets/btc-clp/ticker')
    .map((response: Response) => response.json());
  }

}

Answer №1

this.minask is not a direct value, but rather a method. Therefore, the error you are encountering is due to this reason. To resolve this, update your code to call the method like so:

this.minask($YOUR_ARGUMENT)

To see how to use this in your component, here is an example snippet:

demoStockViewInstance = {
  last_price : 10;
  min_ask : 5;
  max_bid : 10;
  volume : 10;
  price_variation_24h : 10;
  price_variation_7d : 10;
}
// exchange should have a value of 5 
exchangeRate = this.minask(this.demoStockViewInstance);
targetAmount = 1;
baseAmount = this.exchangeRate;

update(baseAmount) {
 this.targetAmount = parseFloat(baseAmount) / this.exchangeRate;
}

UPDATE:

If the service returns an observable, make sure to calculate within its subscribe callback in your component:

this.surbtcService.getPricess().subscribe(response => {
  demoStockViewInstance = response.min_ask
})

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

Angular 2 and Strophe XMPP Integration

I am currently looking to develop a service for the Strophe.JS XMPP library within an Angular2 project. This service will handle the opening and closing of connections, as well as include a message handler. I have successfully implemented this in a .Net MV ...

Step-by-step guide to designing a basic pagination/carousel using div elements in HTML and Angular 2

Hey there! I'm currently working on a project with an *ngFor in my template that generates multiple divs. <div *ngFor="let item of widgets" class="page-content widget-item"> <div>{{content}} </div> </div> I'd like to i ...

The dramatist strategically positioning the cursor at the conclusion of an input field

Currently, I am utilizing playwright for my testing purposes and have encountered a specific issue that I am seeking assistance with. The behavior I need to test is as follows: Applying the bold style to existing text within my input field Verifying that ...

Error: A stream was expected, but you provided 'undefined'. Please provide either an Observable, Promise, Array, or Iterable instead

I'm encountering an error while trying to catch errors in my Ionic-based application with Angular. In the create() method, I am attempting to create a new User. If the username already exists, I receive a response from the backend, but my method thro ...

CoteJS encounters issues when attempting to generate multiple services

Creating multiple services using cote.js has been a challenge for me. I have around 40 services, each in its own file. The issue arises when I try to start a third service or all of them at once; they stop working, including the two that were functioning p ...

Is it possible to retrieve the signature for every method within a class?

Let's consider a scenario where we have a class defined as follows: class Actions { static FooAction = 'foo' as const; someAction1() { return { type: Actions.FooAction, payload: {a: 1, b:2} }} static BarAction = &apos ...

Is it feasible to arrange tables in ng-bootstrap Sortable Tables by default column selection?

Currently, I am in the process of developing an Angular project that utilizes the ng-bootstrap Sortable Table component. This particular component can be found at the following link: My issue lies in the fact that I require the table to automatically sort ...

I'm encountering an issue with one of my routes not loading correctly in Angular 4 Universal

I have been working on implementing Universal and I believe I've made significant progress. My project is built on this seed. However, when I run "npm start", only the /about and /contact pages render successfully. The /home page does not render at al ...

Utilizing the useState Hook in Reactjs for Object Management with Interfaces

Here are some of the interfaces I have defined: export interface IManuscript { heading: IManuscriptHeading; abstract: IShortSection; sections: ISection; } interface IManuscriptHeading { title: string; authors: Array<IAuthors>; } ...

Can you tell me how to add a custom CSS class to a cell in ag-grid using Angular?

I am facing a challenge with applying CSS to cells in ag-grid in Angular based on specific logic. I have assigned an object to the grid, where one of the fields contains a value for Object.hours and I need to apply styling based on the Object.status proper ...

How to incorporate a popup modal in your project and where should you place the DialogService constructor

Currently, I am in the process of developing a CRUD ASP.NET Core application using Angular 2 and Typescript. Prior to incorporating a popup feature, this was my output: https://i.stack.imgur.com/vHvCC.png My current task involves placing the "Insert or e ...

Loading CSS files from the Assets folder in Angular 2/4: Step-by-step guide

I have a question regarding my .angular-cli.json file configuration. Here is what I have defined: "styles": [ "node_modules/bootstrap/dist/css/bootstrap.min.css", "src/assets/css/main.css", "styles.css" ], In my assets folder, I have CSS files loca ...

In React Native, styled-components do not inherit props that are passed through the attrs method

Our project was recently updated to target RN072.5 and now uses the latest version of styled-components 6.0.8 "dependencies": { ..., "react": "18.2.0", "react-is": "18.2.0", "react-native" ...

The call to styled-components does not match any overload of this function

Many questions have been asked on SO about this error, but none seem to address my specific situation. Despite trying various solutions, nothing has worked for me so far, prompting me to create a new and more targeted question. My inquiry is related to ut ...

Issue encountered while deploying Node.js server on Render platform

I'm currently facing an issue while attempting to deploy a TypeScript server with Node.js to Render. I've provided the directory structure and deployment information below. Any insights or opinions you can offer would be greatly appreciated. Than ...

What is the best method for choosing visible elements within a scrollable container?

Is there a way to retrieve the list of visible elements within a scrollable container? The number of elements that are visible on the screen changes as one scrolls, making it challenging to add a specific class to only the last two visible elements. Any s ...

Is it possible to implement Angular 7 routing in conjunction with AEM 6.5 integration?

My experience involves successfully integrating AEM 6.5 with angular 7. Upon integration, I discovered that angular components are primarily used for rendering by linking AEM components to Angular. We specify the components to be loaded on an AEM page, w ...

The captured image remains intact even after the camera event has occurred

I am currently adding a camera feature to my app. The camera is functioning properly as it opens and allows me to take pictures. However, the issue arises when I press "use picture" and expect the image to appear on a card. Instead, there is just an empty ...

Is it possible to dynamically adjust the size of the CircleProgressComponent element in ng-circle-progress?

For my current Angular 11 project, I am facing the challenge of dynamically changing the size of the ng-circle-progress library's CircleProgressComponent element. After some research, I discovered that the element's size can be adjusted by apply ...

Is there a way for me to access the mp3 files stored in MongoDB through GridFS and read them?

Here is the code I wrote: storeAudio.js var mongoose = require('mongoose'); var fs = require('fs'); var Grid = require('gridfs-stream'); Grid.mongo=mongoose.mongo; //establishing a connection with mongoDB mongoose.Promise ...