Having trouble retrieving prices using an npm package

There is a more effective way to retrieve prices using the npm package, node-binance-api, rather than relying on the "coin" variable that I am currently struggling with. If anyone could assist me in finding a better solution or the optimal method for fetching prices using this package within Discord, it would be greatly appreciated. I have been stuck on this problem for quite some time now.

Here's what I'm looking for:

I need to replace "ticker.XRPBTC" (see code below).

For instance, if I input ETH, the goal is to change from ticker.XRPBTC to ticker.ETHBTC

var coin = (message.content.toUpperCase()).slice(2) + "BTC";

binance.prices((error, ticker) => {

  console.log("Price of " + coin + ":", ticker.XRPBTC);
});

In an attempt to achieve this, I created the variable "coin," thinking that I could simply write ticker.coin but unfortunately, it does not work...

I have experimented with the following:


As an example:

ticker.XRPBTC     - This code works, output: the actual price of the currency.

What I am trying:

var coin = XRPBTC 

console.log(ticker.coin)  - output: undefined 

Moreover, when testing in the console by writing the following, I encounter an error:


  if (msg.startsWith ("eth")) {
    message.reply ("Price of " + coin + ":", ticker.TRXBTC);
  }

An explanation of the functionality of "ticker":

The ticker serves the purpose of obtaining the most recent price for each currency, such as ETHBTC, XRPBTC, TRXBTC, etc. The currency pairs are represented here by eth, xrp, and trx. Therefore, in order to fetch the price for a specific pair, I must format it accordingly within the ticker function.

As demonstrated in the provided code snippet, I am structuring the text precisely as required by the ticker for accurate retrieval.

Hence, when I input eth, the variable "coin" automatically transforms it into ETHBTC

Thank you!

Answer №1

If you're searching for information on accessing properties, you may need to first verify if the coin actually exists.

let coin = message.content.toUpperCase().slice(2) + "BTC";
console.log(coin); // for example, logs "ETHBTC"
binance.prices((error, ticker) => {
  if (coin in ticker) {
    let price = ticker[coin]; // <- accessing property
    message.reply("Price of " + coin + ": " + price);
  } else {
    // handle errors such as:
    message.reply("Coin " + coin + " does not exist");
  }
});

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

Having trouble with Javascript/ajax/php: data is successfully sent from server to client, but client to server communication is not working as

Apologies for the duplicate post (Admins, kindly remove the other one!). I've been receiving great assistance from you all and was hoping to seek your help once again with the following question: I am currently working on implementing AJAX by allowin ...

How to automatically refresh a page in AngularJS after a POST request

After making a POST request, I attempted to use $state.reload in my controller to refresh the page. However, it is not updating the data on my page after registering the form. I have to manually refresh the page to see the correct data. .controller(' ...

Error: The value is null and cannot be read

My external application is set up within a const called setupRemote, where it starts with the appConfig in the variable allowAppInstance. export const setupRemote = () => { if (isRemoteAvailable) { try { ... const allowAppInstance = S ...

Guidelines for creating a dynamic filter in Prisma js

I am looking to create a dynamic filter based on user input from the frontend. On mapping the data, I found that the object results appear like this: { id: '2', name: 'yuhu' } The keys 'id' and 'name' need to be dyn ...

Modify a quartet of divs simultaneously

I am currently working on a project that involves 4 input fields, each accompanied by a dropdown element for selecting currency. My goal is to create a JavaScript function that will update all input fields when one of them is changed. For instance, if I s ...

Determine the total number of selected items in MagicSuggest when it loses focus

I have been working with MagicSuggest and I am currently facing an issue where I need to retrieve the length of selections on a blur event. Interestingly, my code functions perfectly fine when a new selection is added using the ENTER key, but it fails when ...

Ways to automatically update property value in MongoDB once a particular date is reached

Is it feasible to schedule a future date for a document in MongoDB, such as 30 days from the current date, and then automatically update another property of the document when that future date arrives? For instance: creating an event document setting the ...

Efficient techniques for extracting data from several forms simultaneously

I've set up dropzone js for drag and drop file uploads in Django. For this, I needed to use a Django form with the dropzone class. Since I also wanted users to add text information, I ended up creating two forms in Django - one for the dropzone upload ...

Adjust dimensions of images in Bee3D carousel

I recently utilized the Bee3d library available on the following Github link: https://github.com/lukeed/bee3d While I found everything to be truly impressive, I am curious if there is a way to adjust the image size. Whenever I attempt to change the image ...

The addition of an asynchronous call caused Array.map to start experiencing errors

I am working on a React component that iterates through an array of messages and renders JSX based on certain conditions: messages.map(async (msg) => { let previewImage: URL | undefined = undefined; if (msg.mediaId) { previewImage = await stora ...

Modifying the display toggle functions

How can I toggle between a button and an input type "file" when clicked? I am using jQuery to show and hide elements, but I need help changing back to the button when clicking somewhere else on the page. HTML: Upload<input type="button" id="upload" /& ...

Dormant versus dynamic PHP MySQL operations

I am facing an issue where the state doesn't change from active to inactive or vice versa when I click on it. Below is my code for ajax: <script src="//code.jquery.com/jquery-1.10.2.min.js"></script> <script type="text/javascript"> ...

A guide on updating a boolean field in the database using ajax

Here is a piece of my html code: <form action="" method="post"> {% csrf_token %} {% if donate.is_taken == False %} <br> <button type="submit" class="btn" name="taken_or_not" ...

"Once the initial date has been selected, v-Calendar's datepicker allows for setting a

Is there a way to trigger an event for the date range picker of v-calendar after the first date is picked or prevent the inputs from adding the dates until both dates have been selected? Here is the Vue component I have: new Vue({ el: "#app", data( ...

ViewContainerRef fails to render component on the DOM

@Component({ selector: 'my-cmp', template: ` <div #target></div> ` }) export class MyCmp { @ViewChild('target', {read: ViewContainerRef}) target : ViewContainerRef; render() { let component = createComponent(met ...

halt execution of npm test and erase any printed content

When I run npm test on my React project, it runs unit tests using jest and react testing library. The test logs (including console log lines added for debugging) are printed to the screen but get deleted after running the tests. It seems like the logs are ...

What situations call for the use of 'import * as' in TypeScript?

Attempting to construct a cognitive framework for understanding the functionality of import * as Blah. Take, for instance: import * as StackTrace from 'stacktrace-js'; How does this operation function and in what scenarios should we utilize imp ...

Creating a loading screen in Angular2: Step-by-step guide

How can you best integrate a preloader in Angular 2? ...

Enhance Material UI with custom properties

Is it possible to add custom props to a Material UI component? I am looking to include additional props beyond what is provided by the API for a specific component. For example, when using Link: https://material-ui.com/api/link/ According to the document ...

Is it possible to determine the duration of a JQuery AJAX call's execution?

Is it possible to measure the duration of a $.getJSON method call using timing code in jQuery/JavaScript? Additionally, is there a method to determine the size of the response Content-Length, measured in kilobytes or megabytes? ...