如果我不使用包装代码的以下功能,则发布的代码将起作用:
watcher.on('add', function (path) {
});
但是如果运行下面的代码。在path.join不是函数的情况下,我确实收到此错误。 如所见,我一直想在添加文件后一直在watcher.on内运行代码。添加文件时,代码确实会做出反应,但同样,我总是收到以下错误消息。 我是将watcher.on代码设置为错误还是导致错误的原因?
文件C:\ myproject \ instances \ b53pd4574z8pe9x793go \ New Text Document.txt已添加 (节点:14360)UnhandledPromiseRejectionWarning:TypeError:path.join不是函数
完整代码:
'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`)
})()
});