0

When i run my project in terminal like this node index.js

I get a reference error: config not defined

/home/ether/Documents/nodesendeth/index.js:6 const {asset, base, spread, allocation} = config; ^

ReferenceError: config is not defined
    at tick (/home/ether/Documents/nodesendeth/index.js:6:47)
    at run (/home/ether/Documents/nodesendeth/index.js:49:3)
    at Object.<anonymous> (/home/ether/Documents/nodesendeth/index.js:52:1)
    at Module._compile (internal/modules/cjs/loader.js:1151:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1171:10)
    at Module.load (internal/modules/cjs/loader.js:1000:32)
    at Function.Module._load (internal/modules/cjs/loader.js:899:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12)
    at internal/main/run_main_module.js:17:47

in my code i have assigned the config constant like this:

require('dotenv').config;
const ccxt = require('ccxt');
const axios = require('axios');

const tick = async => {
    const {asset, base, spread, allocation} = config;
    const market = `${asset}/${base}`;

    const orders =  binanceClient.fetchOpenOrders(market);
    orders.forEach(async order => {
        await binanceClient.cancelOrder(order.id);
    });
    const results =  Promise.all([
      axios.get('https://api.coingecko.com/api/v3/simple/price?ids=bitcoin&vs_currencies=usd'),
      axios.get('https://api.coingecko.com/api/v3/simple/price?ids=tether&vs_currencies=usd')
    ]);
    const marketPrice = results[0].data.bitcoin.usd / results[1].data.tether.usd;

    const sellPrice = marketPrice * (1 + spread);
    const buyPrice = marketPrice  * (1 - spread);
    const balances = binanceClient.fetchBalance();
    const assetBalance = balances.free[asset];
    const baseBalance = balances.free[base];
    const sellVolume = assetBalance * allocation;
    const buyVolume = (baseBalance * allocation) / marketPrice;

    binanceClient.createLimitSellOrder(market, sellVolume, sellPrice);
    binanceClient.createLimitBuyOrder(market, sellVolume, buyPrice);

     console.log(`
        New tick for ${market}...
        Created limit sell order for ${sellVolume}@${sellPrice}
        Create limit buy order for ${buyVolume}@${buyPrice}`)
}

const run = () => {
    const config = {
      asset: 'BTC',
      base: 'USDT',
      allocation: 0.1,
      spread: 0.2,
      tickInterval: 2000
    };
  const binanceClient = new ccxt.binance({
    apiKey: process.env.API_ENV,
    secret: process.env.API_SECRET,
  });

  tick(config, binanceClient);
  setInterval(tick, config.tickInterval, config, binanceClient);
}
run()

I also got await errors saying that i need to run them in an sync function which I do. i have removed the await keyword now but the app should run with the await kewyword because those functions are async.

Why am i getting those errors when i have a config variable aswell as an async function?

I assume that something with my async function doesnt work because it neither recognises the config constant nor the async/await calls

4
  • const {asset, base, spread, allocation} = config; What do you think config is in this line? Commented Nov 9, 2020 at 20:49
  • Which module do {asset, base, spread, allocation} come from? Commented Nov 9, 2020 at 20:57
  • i dont know which module you mean Commented Nov 9, 2020 at 21:13
  • Are those environment variables that you expect to have be defined in your .env file? Commented Nov 9, 2020 at 21:16

1 Answer 1

1

config is undefined in your code.

You likely meant to declare it on this line, as follows:

const config = require('dotenv').config();

...but this is only half of your problem because, furthermore, the object returned by .config() puts all the variables in a parsed property.

So for example, if your .env looked like this:

apple=red
ball=blue

...and you just logged config, you'd see this:

{ parsed: { apple: 'red', ball: 'blue' } }

Then you could write:

const {apple, ball} = require('dotenv').config().parsed;
console.log(apple, ball);

And this would write:

red blue

In your specific case, if you are expecting asset, base, spread, and allocation to be environment variables in your .env file, then you could potentially change the line to say:

const {asset, base, spread, allocation} = config.parsed;
Sign up to request clarification or add additional context in comments.

2 Comments

ok, this fixed the above error but it still errors out on all my constant, now it is saying i didnt declare binanceClient, when it is declared. and my async/await errors are still here. Am i not making a mistake somewhere else
I'm guessing a lot here because you haven't fully disclosed what you're trying to do, but based on my guesses I've added more information that will hopefully let you fix things up.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.