简体   繁体   English

Javascript - 错误:ENOENT:没有这样的文件或目录

[英]Javascript - Error: ENOENT: no such file or directory

I am currently working on a project containing several functions from various files in my project.我目前正在开发一个项目,其中包含来自我项目中各种文件的多个函数。 I exported them with:我将它们导出为:

module.exports.nomFonction= function{...}

I would like to compile everything in an index.js file that would call all these functions, here is where I am (rudimentary code that I am looking to improve):我想在 index.js 文件中编译所有可以调用所有这些函数的文件,这就是我所在的位置(我希望改进的基本代码):

var fs = require('fs');
const mt = require("./mainTweet"); // from mainTweet.js
const rp = require("./Reply"); // from Reply.js
const od = require("./getcsv"); // from Reply.js

 async function sendTweet(err, data) {
    if (err) {
      console.log(err)
    } else {
        await od.onDownload();
        await mt.mainTweet();
        await rp.Reply();
    }
} 

sendTweet();

The first function onDownload (code below) allows to download a file each time it is updated.第一个 function onDownload(代码如下)允许在每次更新文件时下载文件。 When I launch the sendTweet function, this error appears: Error: ENOENT: no such file or directory, open 'files/data.csv'.当我启动 sendTweet function 时,出现此错误:错误:ENOENT:没有此类文件或目录,打开“文件/数据.csv”。 She works when i call it lonely but not in index.js, however i added a waiting time between the execution of the first and second function.getcsv.js (onDownload):当我称之为孤独但不在 index.js 中时,她工作,但是我在第一个和第二个 function.getcsv.js (onDownload)的执行之间添加了一个等待时间:

const fetch = require('isomorphic-fetch');
const converterCsvToJson = require("csvtojson");
const fs = require('fs');
const path = require('path');

// created a folder to store the downloaded files
const PATH_FOLDER_DOWNLOAD = path.join(__dirname, "./files");

if(!fs.existsSync(PATH_FOLDER_DOWNLOAD)) {
    // if the download folder does not exist we create it
    fs.mkdirSync(PATH_FOLDER_DOWNLOAD);
}

async function onDownload() {
    // get the link of the last CSV file
    const response = await fetch('https://www.data.gouv.fr/api/2/datasets/6010206e7aa742eb447930f7/resources/?page=1&type=main&page_size=1', {
        method: "GET"
    });

    const data = await response.json();
    // console.log(data)

    const lastItem = data.data[0];

    // check if the file is not already owned, using a hash to uniquely identify the data
    const integrity = 'data';

    // Using simple `fs.readdirSync` would give you filenames as well as directories.
    // This approach creates objects called `Dirent`s where you can check if the the entry is a file or a directory.
    // Then you can simply filter out the directories and get the filenames.
    const filenames = fs.readdirSync(PATH_FOLDER_DOWNLOAD, {withFileTypes: true, encoding: "utf-8"})
        .filter(dirent => dirent.isFile())
        .map(dirent => dirent.name);

    // Check if some of the filenames contains the integrity hash
    const exists = filenames.some(filename => filename.indexOf(integrity) !== -1);

    // we already have this file it has not been updated yet we give up the operation
    if(exists) {
        const {published} = lastItem;
        console.log(`operation cancelled, no file has been published since: ${published}`);

    } else {
        // we don't own this file we download it
        const urlCsvFile = lastItem.url;
        const response = await fetch(urlCsvFile, {method: "GET"});
        const csvContent = await response.text();

        // writes the new file to the download folder
        // using the hash as the file name, which will allow later to check if you already have it

        fs.writeFileSync(
            path.join(PATH_FOLDER_DOWNLOAD, (integrity + ".csv")),
            csvContent,
            {encoding: "utf-8"}
        );

        // the CSV file has been downloaded and saved in a file at: /download/{hash}.csv
        console.log('File downloaded!')
    }
}

// onDownload();

So Im looking for a solution to correct this problem, i tried some tricks usings different docs but nothing works Good evening and thank you in advance for your help所以我正在寻找解决这个问题的解决方案,我尝试了一些使用不同文档的技巧,但没有任何效果 晚上好,提前感谢您的帮助

Looks to me that the PATH_FOLDER_DOWNLOAD variable is not initialized as expected, probably because of __dirname variable not being initialized.在我看来,PATH_FOLDER_DOWNLOAD 变量未按预期初始化,可能是因为 __dirname 变量未初始化。 Take a look of this post regarding __dirname看看这篇关于 __dirname的帖子

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM