Node.js SyntaxError:await仅在Google Cloud Speech to Text的异步函数中有效
node.js和Google云工具都有点业余。我用这段代码试图从我的谷歌云存储桶中转录一段音频。当我在终端中运行Node.js SyntaxError:await仅在Google Cloud Speech to Text的异步函数中有效,node.js,terminal,Node.js,Terminal,node.js和Google云工具都有点业余。我用这段代码试图从我的谷歌云存储桶中转录一段音频。当我在终端中运行node index.js时,我只得到SyntaxError:await仅在异步函数中有效。我意识到这意味着我需要一个异步函数。但我如何才能将整个文件转换为可以从终端成功运行的命令 //导入Google云客户端库 const speech=require(“@googlecloud/speech”); //创建一个客户端 const client=new speech.SpeechC
node index.js
时,我只得到SyntaxError:await仅在异步函数中有效。我意识到这意味着我需要一个异步函数。但我如何才能将整个文件转换为可以从终端成功运行的命令
//导入Google云客户端库
const speech=require(“@googlecloud/speech”);
//创建一个客户端
const client=new speech.SpeechClient();
/**
*TODO(开发人员):在运行示例之前取消注释以下行。
*/
const gcsUri='gs://anitomaudiofiles/911isnojoke.mp3';
常量编码='MP3';
恒采样频率=16000赫兹;
常量语言代码='en US';
常量配置={
编码:编码,
sampleRateHertz:sampleRateHertz,
languageCode:languageCode,
};
常量音频={
uri:gcsUri,
};
常量请求={
config:config,
音频:音频,
};
//检测音频文件中的语音。这将创建一个您需要的识别作业
//可以现在等待,也可以稍后获得结果。
const[operation]=等待客户端。LongRunningRecognite(请求);
//获得工作最终结果的承诺陈述
const[response]=等待操作。promise();
常量转录=响应。结果
.map(result=>result.alternations[0]。转录本)
.join('\n');
log(`Transcription:${Transcription}`)代码>只需将其放入异步函数中:
// earlier code here
async function main() {
const [operation] = await client.longRunningRecognize(request);
const [response] = await operation.promise();
const transcription = response.results
.map(result => result.alternatives[0].transcript)
.join('\n');
console.log(`Transcription: ${transcription}`);
}
main()
您不能在异步函数之外编写等待操作.promise()
。如果您想使用wait,它应该在函数中
(async runOperations() {
const [operation] = await client.longRunningRecognize(request);
// Get a Promise representation of the final result of the job
const [response] = await operation.promise();
const transcription = response.results
.map(result => result.alternatives[0].transcript)
.join('\n');
console.log(`Transcription: ${transcription}`);
})();
您可以将其放入文件中并运行节点
来运行它