Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/385.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用NPMExiftool(javascript)异步问题_Javascript_Npm_Exiftool - Fatal编程技术网

使用NPMExiftool(javascript)异步问题

使用NPMExiftool(javascript)异步问题,javascript,npm,exiftool,Javascript,Npm,Exiftool,我在使用npm exiftool时遇到问题。我正试着用它做一些事情 通过特定文件夹迭代图像文件 获取每个图像文件的“xpKeywords”数据 写入存储数据的文件 这是我的密码 const fs=require('fs'); const exif=require('exiftool'); const folderName='testImages'; const inputPath=`/Users/myName/Project/Project/${folderName}`; const file

我在使用npm exiftool时遇到问题。我正试着用它做一些事情

  • 通过特定文件夹迭代图像文件
  • 获取每个图像文件的“xpKeywords”数据
  • 写入存储数据的文件
  • 这是我的密码

    const fs=require('fs');
    const exif=require('exiftool');
    const folderName='testImages';
    const inputPath=`/Users/myName/Project/Project/${folderName}`;
    const files=fs.readdirSync(inputPath,'utf8');
    让数据=[];
    (异步()=>{
    让承诺=[];
    files.forEach(file=>promises.push(fs.readFileSync(`${inputPath}/${file}'));
    让结果=等待承诺。全部(承诺);
    for(让results.entries()的[索引,结果]){
    让数据=等待获取元数据(结果、索引);
    控制台。登录(“退出”);
    数据推送(数据);
    }
    fs.writeFileSync('outputData/metaData.json',json.stringify(data,null,4),(错误)=>{
    console.log('发生错误');
    });
    })();
    异步函数getMetadata(结果、索引){
    log(`get metadata${index}`);
    等待exif.metadata(结果,(错误,元数据)=>{
    返回{
    名称:文件[索引],
    hashTags:metadata.xpKeywords
    };
    });
    
    }
    这里有几个问题

    • 主要的一点是
      getMetaData
      不返回任何内容。从给定的回调返回
      exif.metadata
      不会设置
      getMetaData
      的返回值
    • 另外,不返回承诺,因此,
      let results=wait promise.all(承诺)没有意义呼叫。(顾名思义,它是同步工作的)
    • 通过索引将文件引用到全局模块中容易出错
    要使用Promissions,您可以使用Node.js当前的实验版本,或者在
    fs.readFile
    上使用(这是一个使用回调的实际异步版本)。如果您想获得启用承诺的版本,还可以在
    exif.metadata
    上使用
    promisify

    详细介绍如何将基于回调的API转换为承诺

    记住所有这些,我认为您正在沿着这些路线寻找一些东西(可能需要调整):

    const fs = require('fs');
    const {promisify} = require('util');
    const exif = require('exiftool');
    
    const pReaddir = promisify(fs.readdir);
    const pReadFile = promisify(fs.readFile);
    const pWriteFile = promisify(fs.writeFile);
    const pMetadata = promisify(exif.metadata);
    
    const folderName = 'testImages';
    const inputPath = `/Users/myName/Project/project/${folderName}`;
    
    (async () => {
        // Get the file names
        const files = await pReaddir(inputPath, 'utf8');
        // Process the files in parallel and wait for the result
        const data = Promise.all(files.map(async(name) => {
            // Read this file and get its metadata from it
            let {xpKeywords: hashTags} = await pMetadata(await pReadFile(`${inputPath}/${name}`));
            // Return an object with the file's name and xpKeywords as its hashTags
            return {name, hashTags};
        }));
        // Write the results to file
        await pWriteFile('outputData/metaData.json', JSON.stringify(data, null, 4));
    })()
    .catch(error => {
        console.error(error);
    });