尝试在POST请求中使用Google语音云时出现ReadFileSync错误

问题描述

我对Node.js相当陌生,一直在尝试让我的应用程序与Google语音云一起工作,但遇到了一些麻烦。首先,我使用发帖请求在我的网站上创建了文件上传表单,然后我通过以下代码立即将其提交给Google云:

app.post('/submit-form',multer({storage: storage}).single('audiofile'),function(req,res) {
  console.log(req.file);
  async function main() {
    // Creates a client
    var client = new speech.SpeechClient();
   
    // The name of the audio file to transcribe
    var fileName = path.join(__dirname,'./uploads/' + req.file.filename);
    console.log(fileName)
   
    // Reads a local audio file and converts it to base64
    var file = fs.readFileSync(fileName);
    var audioBytes = file.toString('base64');
   
    // The audio file's encoding,sample rate in hertz,and BCP-47 language code
    var audio = {
      content: audioBytes,};
    var config = {
      encoding: 'LINEAR16',sampleRateHertz: 16000,languageCode: 'en-US',};
    var request = {
      audio: audio,config: config,};
   
    // Detects speech in the audio file
    var [response] = await client.recognize(request);
    var transcription = response.results
      .map(result => result.alternatives[0].transcript)
      .join('\n');
    console.log(`Transcription: ${transcription}`);
    var fs = writeFile('./resources/results/result.txt',transcription);
  }
});

麻烦的是,由于某种原因,程序总是卡在readFileSync上,

TypeError: Cannot read property 'readFileSync' of undefined 即使我提交的文件名已经在文件上传中,代码也不起作用,所以我不确定什么地方出错了

解决方法

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

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

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