7

如果我拿走我用以下包装代码的函数,我发布的代码就可以工作:

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

但是如果运行下面的代码。我确实在 path.join 不是函数的情况下收到此错误。正如所见,我想在添加文件时一直运行 watcher.on 中的代码。当我添加文件时,代码会做出反应,但我总是收到以下错误。我是否设置了 watcher.on 代码错误或导致错误的原因?

文件 C:\myproject\instances\b53pd4574z8pe9x793go\New Text Document.txt 已添加 (node:14360) UnhandledPromiseRejectionWarning: TypeError: path.join is not a function

完整代码:

'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`)
    })()
});

4

1 回答 1

0

我认为,这是因为您将“路径”作为参数发送,同时尝试从“路径”调用 join()。重命名“函数(路径)”中的输入参数,我认为这会有所帮助。

于 2022-01-05T08:47:13.187 回答