如何解决尝试在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 举报,一经查实,本站将立刻删除。