如何运行exif函数异步并等待结果?

问题描述

我无法运行EXIF.js并等待结果。

exif的用法是:

function getExif() {
    var img1 = document.getElementById("img1");
    EXIF.getData(img1,function() {
        var make = EXIF.getTag(this,"Make");
        var model = EXIF.getTag(this,"Model");
        var makeAndModel = document.getElementById("makeAndModel");
        makeAndModel.innerHTML = `${make} ${model}`;
    });

    var img2 = document.getElementById("img2");
    EXIF.getData(img2,function() {
        var allMetaData = EXIF.getAllTags(this);
        var allMetaDataSpan = document.getElementById("allMetaDataSpan");
        allMetaDataSpan.innerHTML = JSON.stringify(allMetaData,null,"\t");
    });
}

但是,事情是我需要等待exif返回数据,然后再将其传递给使用数据创建卡的组件。

我的代码是:

async function filedata(i){
  const selectedFile = document.getElementById('input').files[i];
  EXIF.getData(selectedFile,async function Metadata() {
    if (this.exifdata.GPSLatitude !== undefined && this.exifdata.GPSLongitude !== undefined) {
    return {
          cardId: selectedFile.name,imageUrl: window.URL.createObjectURL(selectedFile),size: selectedFile.size,lat: calculateGpsDatalat(selectedFile),lon: calculateGpsDatalon(selectedFile),}
    }}
  )
 let data = await Metadata();
 return data;
}

当然会给出错误

Failed to compile.

./src/App.js
  Line 29:19:  'Metadata' is not defined  no-undef     

Search for the keywords to learn more about each error.

问题是如何编写此函数以等待来自Exif函数的数据。

谢谢。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)