首页 > 解决方案 > 由于回调、异步和等待而面临错误

问题描述

我需要使用回调来获取目录中的文件并使用我的函数处理它们

async function getFilesFromDirectory(path, callback) {
  await fs.readdir(path, function (err, content) {
    if (err) return callback(err);
    callback(null, content);
  });
}

checkVideo = async (filePath) => {
  let folderpath = path.join(__dirname, "../videoScreenshots");
  let result = await getFilesFromDirectory(folderpath, async (err, files) => {
    let finalVal = [];
    finalVal = await filereader(files);
    console.log("final checked vals :", finalVal);
    return finalVal;
  });
  console.log("result:", result);
};

但我不知道为什么,异步等待似乎没有在回调上工作,我需要将最终检查的 vals(稍后打印)返回到变量结果中,以便我可以将其进一步返回到另一个函数中。我已经尝试了很长一段时间,但它似乎不起作用.. 请帮助我.. :(

第一行..结果:未定义

[0] result: undefined
[0] D:\College\Capstone\CAPSTONE-Dev-Prototype\nodeapi\videoScreenshots\thumbnail-at-12.185599999999999-seconds.png
[0] D:\College\Capstone\CAPSTONE-Dev-Prototype\nodeapi\videoScreenshots\thumbnail-at-12.185599999999999-seconds.png
[0] node-pre-gyp info This Node instance does not support builds for N-API version 7
[0] node-pre-gyp info This Node instance does not support builds for N-API version 8
[0] node-pre-gyp info This Node instance does not support builds for N-API version 7
[0] node-pre-gyp info This Node instance does not support builds for N-API version 8
[0] 2021-06-02 22:49:41.566584: I tensorflow/core/platform/cpu_feature_guard.cc:142] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN) to use the following CPU instructions in performance-critical operations:  AVX2
[0] To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.
[0] D:\College\Capstone\CAPSTONE-Dev-Prototype\nodeapi\videoScreenshots\thumbnail-at-18.278399999999998-seconds.png
[0] D:\College\Capstone\CAPSTONE-Dev-Prototype\nodeapi\videoScreenshots\thumbnail-at-18.278399999999998-seconds.png
[0] D:\College\Capstone\CAPSTONE-Dev-Prototype\nodeapi\videoScreenshots\thumbnail-at-24.371199999999998-seconds.png
[0] D:\College\Capstone\CAPSTONE-Dev-Prototype\nodeapi\videoScreenshots\thumbnail-at-24.371199999999998-seconds.png
[0] D:\College\Capstone\CAPSTONE-Dev-Prototype\nodeapi\videoScreenshots\thumbnail-at-30.159359999999996-seconds.png
[0] D:\College\Capstone\CAPSTONE-Dev-Prototype\nodeapi\videoScreenshots\thumbnail-at-30.159359999999996-seconds.png
[0] D:\College\Capstone\CAPSTONE-Dev-Prototype\nodeapi\videoScreenshots\thumbnail-at-6.0927999999999995-seconds.png
[0] D:\College\Capstone\CAPSTONE-Dev-Prototype\nodeapi\videoScreenshots\thumbnail-at-6.0927999999999995-seconds.png
[0] { violence: 78.71493101119995, nudity: 60.231149196624756 }
[0] { violence: 98.11700582504272, nudity: 45.4850971698761 }
[0] { violence: 97.38714694976807, nudity: 87.78105974197388 }
[0] { violence: 98.76883029937744, nudity: 8.434739708900452 }
[0] { violence: 76.18809938430786, nudity: 61.19433045387268 }
[0] final checked vals : [
[0]   { violence: 78.71493101119995, nudity: 60.231149196624756 },
[0]   { violence: 98.11700582504272, nudity: 45.4850971698761 },
[0]   { violence: 97.38714694976807, nudity: 87.78105974197388 },
[0]   { violence: 98.76883029937744, nudity: 8.434739708900452 },
[0]   { violence: 76.18809938430786, nudity: 61.19433045387268 }
[0] ]

标签: javascriptnode.jsasynchronousasync-awaitcallback

解决方案


推荐阅读