对于未发送到后续函数的每个循环值.这里发生了什么?Node.js



我已经在这个项目上工作了三天,我几乎已经了解了我正在做的一切,以及为什么一个值会抛出错误,但在这个代码中:

function updateStats(){
const users = readFile('users','txt','UTF-8');
users.forEach((v) => {    
console.log(v);
var user =  v.toString();
const statFile = readFile(user,'txt','UTF-8');
readHTTP(v,currentChannel,statFile[0]);
});
}

抛出错误:

I am doing my 15 minutes update
610636905440215071
677257430249242655
539899963568553986
525819952708583427
373982165307752460
Error: ENOENT: no such file or directory, open '/Users/codel/OneDrive/Documents/BattlEye/files/.txt'
at Object.openSync (fs.js:476:3)
at Object.readFileSync (fs.js:377:35)
at readFile (C:UserscodelOneDriveDocumentsBattlEyeindex.js:98:25)
at C:UserscodelOneDriveDocumentsBattlEyeindex.js:214:26
at Array.forEach (<anonymous>)
at updateStats (C:UserscodelOneDriveDocumentsBattlEyeindex.js:211:11)
at Timeout._onTimeout (C:UserscodelOneDriveDocumentsBattlEyeindex.js:27:3)
at listOnTimeout (internal/timers.js:554:17)
at processTimers (internal/timers.js:497:7) {
errno: -4058,
syscall: 'open',
code: 'ENOENT',
path: '/Users/codel/OneDrive/Documents/BattlEye/files/.txt'
}
C:UserscodelOneDriveDocumentsBattlEyeindex.js:215
readHTTP(v,currentChannel,statFile[0]);
^
TypeError: Cannot read property '0' of undefined
at C:UserscodelOneDriveDocumentsBattlEyeindex.js:215:43
at Array.forEach (<anonymous>)
at updateStats (C:UserscodelOneDriveDocumentsBattlEyeindex.js:211:11)
at Timeout._onTimeout (C:UserscodelOneDriveDocumentsBattlEyeindex.js:27:3)
at listOnTimeout (internal/timers.js:554:17)
at processTimers (internal/timers.js:497:7)

我假设它与函数结构有关,但我的其他foreach循环在这种语法中运行得很好。有人能帮忙调试吗?

编辑:读取文件:

function readFile(fileName,fileType,fileFormat){
try {
const data = fs.readFileSync('/Users/codel/OneDrive/Documents/BattlEye/files/'+fileName+'.'+fileType, fileFormat);
const lines = data.split(/r?n/);
return lines;
} catch (err) {
console.error(err);
}
}

ReadHTTP:

function readHTTP(user,cc,userID){
const puppeteer = require('puppeteer');
var url= 'https://r6.tracker.network/profile/pc/' + userID;
(async() => {
const browser = await puppeteer.launch();
const page = await browser.newPage();
try {
await page.goto(url);
} catch(error){
cc.send('You did not enter a valid Ubisoft UserID');
}
const data = await page.evaluate(() => {
return Array.from(document.querySelectorAll('.trn-defstat__value'))
.map(item => item.innerText);
}); 
writeStatFile(user,userID,data);
console.log("Personal stats file was written");
})();
}

我两天前刚刚学习了这种语言,但值并没有通过for each循环传递到后续函数。

几乎完全可以肯定,users.txt中有一个空换行符

我会做以下事情:

function updateStats(){
const users = readFile('users','txt','UTF-8');
users.forEach((v) => {
if (v === '') return
console.log(v);
var user =  v.toString();
const statFile = readFile(user,'txt','UTF-8');
readHTTP(v,currentChannel,statFile[0]);
});
}

错误号:-4058,syscall:'open',代码:"ENOENT",路径:'/Users/codel/OneDrive/Documents/BattlEye/files/.txt'<--我认为错误来自于这个。。。

寻找异步问题是一个很好的理由。

试试这个:

const promises = require('fs').promises
const updateStats = async () => {
try {
const users = await promises.readFile('users.txt', {encoding: 'utf8'});
for (let user of users.split("n")) {
const statFile = await promises.readFile(user.toString() + '.txt', {encoding: 'utf8'});
if(statFile && statFile[0]) {
readHTTP(user,currentChannel,statFile[0]);
}
}
} catch(err) {
console.error(err.toString())
}
}

不确定这是否会工作,因为我没有时间测试它。

真的希望这有帮助:(

最新更新