I've created a module that is pulling data from binance us api, but I can't seem to send that variable to any other file properly. I'm trying to create a function that pulls a variable each time it runs and send it to be used as a dynamic variable in another file.
This is my updated function code from discussions below in /modules/Tradepair.js:
async function getTradepair() {
const apiUrl = "https://api.binance.us/api/v3/ticker/24hr";
// Make a GET request
let data ;
try{
const response = await fetch(apiUrl);
if (!response.ok) {
throw new Error("Network response for pair was not ok");
}
data = await response.json();
// console.log(data);
} catch (error) {
console.error('There was a problem with the fetch operation:', error);
}
const max = data.reduce(
(a, b) => (a.priceChangePercent > b.priceChangePercent ? a : b), data[0]);
console.log(`Max Symbol from API: ${max.symbol}`);
console.log(`Max Percent from API: ${max.priceChangePercent}`);
const tradingPair = max.symbol;
console.log(`Assigned to Trading pair variable: ${tradingPair}`);
return tradingPair; // Return the tradingPair variable
}
module.exports = getTradepair;
This is the updated version from discussions below:
require("dotenv").config();
const Wolf = require("./modules/Wolf.js");
const getTradepair = require("./modules/Tradepair.js"); //file-name where you have defined this `getTradepair()` function.
async function getData() {
try {
const data = await getTradepair();
console.log("From index:" + " " +data);
return data;
} catch (error) {
console.error("Error fetching data:", error);
}
}
const config = {
// tradingPair: process.env.TARGET_ASSET + process.env.BASE_ASSET,
tradingPair: getData(),
profitPercentage: Number(process.env.PROFIT_PERCENTAGE) / 100,
budget: Number(process.env.BUDGET),
compound: process.env.COMPOUND.toLowerCase() === "true",
profitLockPercentage: Number(process.env.PROFIT_LOCK_PERCENTAGE) / 100,
stopLimitPercentage: Number(process.env.STOP_LIMIT_PERCENTAGE) / 100,
};
const wolf = new Wolf(config);
And this is the error I keep getting:
Max Symbol from API: VITEUSDT
Max Percent from API: 6.467
Assigned to Trading pair variable: VITEUSDT
From index: VITEUSDT
node:internal/process/promises:289
triggerUncaughtException(err, true /* fromPromise */);
^
[UnhandledPromiseRejection: This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). The promise rejected with the reason "false".] {
code: 'ERR_UNHANDLED_REJECTION'
}
Node.js v20.12.1
[nodemon] app crashed - waiting for file changes before starting...
Before the Promise
gets resolved this getData()
is executed in a synchronous manner which is not a valid practice and that is why you are seeing this error.
After defining the getTradepair()
as an async
function import and execute this function like this.
const getTradepair = require('./modules/your-file-name'); //file-name where you have defined this `getTradepair()` function.
async function getData(){
try {
const data = await getTradepair();
console.log(data);
return data ;
} catch (error) {
console.error('Error fetching data:', error);
}
}
getData().then((data) => {
const config = {
// tradingPair: process.env.TARGET_ASSET + process.env.BASE_ASSET,
tradingPair: data,
profitPercentage: Number(process.env.PROFIT_PERCENTAGE)/100,
budget: Number(process.env.BUDGET),
compound: process.env.COMPOUND.toLowerCase() === "true",
profitLockPercentage: Number(process.env.PROFIT_LOCK_PERCENTAGE)/100,
stopLimitPercentage: Number(process.env.STOP_LIMIT_PERCENTAGE)/100
};
const wolf = new Wolf(config);
console.log(wolf); // you can also console to see your expected result
} ).catch((error) => {
console.error("Error:", error);
});
Try the above code.I am sure you will not get this UnhandledPromiseRejection
error. Still if it doesn't go along the way then please don't bother to ask for help.