簡體   English   中英

對於每個未發送到后續 function 的循環值。 這里發生了什么? Node.js

[英]For each loop value not being sent to subsequent function. What is happening here? 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:\Users\codel\OneDrive\Documents\BattlEye\index.js:98:25)
    at C:\Users\codel\OneDrive\Documents\BattlEye\index.js:214:26
    at Array.forEach (<anonymous>)
    at updateStats (C:\Users\codel\OneDrive\Documents\BattlEye\index.js:211:11)
    at Timeout._onTimeout (C:\Users\codel\OneDrive\Documents\BattlEye\index.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:\Users\codel\OneDrive\Documents\BattlEye\index.js:215
        readHTTP(v,currentChannel,statFile[0]);
                                          ^

TypeError: Cannot read property '0' of undefined
    at C:\Users\codel\OneDrive\Documents\BattlEye\index.js:215:43
    at Array.forEach (<anonymous>)
    at updateStats (C:\Users\codel\OneDrive\Documents\BattlEye\index.js:211:11)
    at Timeout._onTimeout (C:\Users\codel\OneDrive\Documents\BattlEye\index.js:27:3)
    at listOnTimeout (internal/timers.js:554:17)
    at processTimers (internal/timers.js:497:7)

我假設它與 function 結構有關,但我的其他 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);
    }
}

讀取HTTP:

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循環傳遞到后面的function。

errno:-4058,系統調用:'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())
  }
}

不確定這是否可行,因為我沒有時間測試它。

真的希望這會有所幫助:)

幾乎完全可以肯定你在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]);
    });
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM