首页 > 解决方案 > SyntaxError : await is only valid in async function in .js program written for use with Google Cloud Speech to Text (Node.js)

问题描述

I have viewed the other questions and answers related to this issue, but in a context = different from mine. I adapted this code from the Google and only changed audio file encoding type, sampling rate and language code. Again, this file is coming directly from Google, so it can't be that I have introduced weird stuff. I have also downloaded the appropriate @google-cloud/speech require needed line 1. Given all this, can someone tell me why this is throwing the error shown in the title of this message related to "await" being valid in async functions?

// Imports the Google Cloud client library
const speech = require('@google-cloud/speech');

// Creates a client
const client = new speech.SpeechClient();

/**
 * TODO(developer): Uncomment the following lines before running the sample.
 */
const gcsUri = 'gs://legrandtimonier_de/mac_test.flac';
const encoding = 'FLAC';
const sampleRateHertz = 44100;
const languageCode = 'de-DE';

const config = {
  encoding: encoding,
  sampleRateHertz: sampleRateHertz,
  languageCode: languageCode,
};

const audio = {
  uri: gcsUri,
};

const request = {
  config: config,
  audio: audio,
};

// Detects speech in the audio file. This creates a recognition job that you
// can wait for now, or get its result later.
const [operation] = await client.longRunningRecognize(request);
//const [operation] = client.longRunningRecognize(request);
// Get a Promise representation of the final result of the job
const [response] = await operation.promise();
const [response] = operation.promise();
const transcription = response.results
  .map(result => result.alternatives[0].transcript)
  .join('\n');
console.log(`Transcription: ${transcription}`);

标签: node.jsgoogle-cloud-speech

解决方案


您不能await在应用程序的根目录中使用方法,它需要在异步函数中使用。尝试将您的代码包装在一个方法中,然后调用它。这是一个例子:

// Imports the Google Cloud client library
const speech = require('@google-cloud/speech');

// Creates a client
const client = new speech.SpeechClient();




const main = async () => {

    /**
     * TODO(developer): Uncomment the following lines before running the sample.
     */
    const gcsUri = 'gs://legrandtimonier_de/mac_test.flac';
    const encoding = 'FLAC';
    const sampleRateHertz = 44100;
    const languageCode = 'de-DE';

    const config = {
        encoding: encoding,
        sampleRateHertz: sampleRateHertz,
        languageCode: languageCode,
    };

    const audio = {
        uri: gcsUri,
    };

    const request = {
        config: config,
        audio: audio,
    };

    // Detects speech in the audio file. This creates a recognition job that you
    // can wait for now, or get its result later.
    const [operation] = await client.longRunningRecognize(request);
    //const [operation] = client.longRunningRecognize(request);
    // Get a Promise representation of the final result of the job
    const [response] = await operation.promise();
    const [response] = operation.promise();
    const transcription = response.results
        .map(result => result.alternatives[0].transcript)
        .join('\n');
    console.log(`Transcription: ${transcription}`);

}



main();

推荐阅读