Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeError: path.join is not a function

The code that I have posted is working if I take AWAY the below function that I have wrapped the code with:

watcher.on('add', function (path) { 
});

But if running the below code. I do get this error where path.join is not a function. As seen I want to all the time run the code inside watcher.on when a file has been added. The code does react when I add a file but again I always receive below error. Do I set up the watcher.on code wrong or what causes the error?

File C:\myproject\instances\b53pd4574z8pe9x793go\New Text Document.txt has been added (node:14360) UnhandledPromiseRejectionWarning: TypeError: path.join is not a function

Complete code:

'use strict';
const ccxt = require('ccxt');
const fs = require('fs');
const path = require('path');
var chokidar = require('chokidar');
var watcher = chokidar.watch('C:/myproject/instances/b53pd4574z8pe9x793go', { ignored: /^\./, persistent: true });


var i;
const exchangename = "binance";
const exchange = new ccxt.binance({
    'enableRateLimit': false
});



watcher.on('add', function (path) {
(async () => {
    console.log('File', path, 'has been added')
    const start = Date.now()


    var orderbookPromises = []
    var symbols = ['ETH/BTC']
    for (i = 0; i < symbols.length; i++) {

        const symbol = symbols[i]
        
        try {
                let tickerProcessing = new Promise(async (resolve) => {
                    const orderbook = await exchange.fetchOrderBook(symbol, 5)

                    const exchangename2 = exchangename + '#' + symbol.replace("/", "")
                    const dumpFile = path.join(__dirname, 'orderbooks', `${exchangename2}Orderbook.txt`)
                await fs.promises.writeFile(dumpFile, JSON.stringify(orderbook))
                resolve()
            })
            orderbookPromises.push(tickerProcessing)

        } catch (e) {
            console.error(e)
        }
    }

    // wait for all of them to execute or fail
    await Promise.all(orderbookPromises)


    const end = Date.now()
    console.log(`Done in ${(end - start) / 1000} seconds`)
    })()
});
like image 867
Andreas Avatar asked Mar 04 '19 22:03

Andreas


People also ask

What is path join ()?

The path. join() method joins the specified path segments into one path. You can specify as many path segments as you like. The specified path segments must be strings, separated by comma.

Is Path join necessary?

Without using it, you usually would make expectations about the start and end of the pathes joined, knowing they only have no or one slash. That sounds a bit useful but receiving arbitrary unchecked paths from unknown sources sounds like a big security problem. That's not something to do often.

Why we use path join?

The path. join() method is used to join a number of path-segments using the platform-specific delimiter to form a single path. The final path is normalized after the joining takes place. The path-segments are specified using comma-separated values.

What is __ Dirname in node JS?

__dirname: It is a local variable that returns the directory name of the current module. It returns the folder path of the current JavaScript file.


1 Answers

I think, it because you send 'path' as a parameter and at the same time, trying to call join() from 'path'. Rename input param in 'function (path)', i think it will help.

like image 116
js_Yuriy Ermolaev Avatar answered Nov 10 '22 01:11

js_Yuriy Ermolaev