【发布时间】:2022-03-08 03:36:16
【问题描述】:
我正在努力调试 NextJS API,该 API 正在开发中(通过 localhost),但默默地在生产中失败。
下面,两个console.log statements 没有返回,所以我怀疑textToSpeech 调用没有正确执行,可能及时吗?
我不确定如何纠正,很高兴按照指示进行调试以解决此问题!
const faunadb = require('faunadb')
const secret = process.env.FAUNADB_SECRET_KEY
const q = faunadb.query
const client = new faunadb.Client({ secret })
const TextToSpeechV1 = require('ibm-watson/text-to-speech/v1')
const { IamAuthenticator } = require('ibm-watson/auth')
const AWS = require('aws-sdk')
const { randomUUID } = require('crypto')
import { requireAuth } from '@clerk/nextjs/api'
module.exports = requireAuth(async (req, res) => {
try {
const s3 = new AWS.S3({
accessKeyId: process.env.AWS_ACCESS_KEY,
secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY
})
const textToSpeech = new TextToSpeechV1({
authenticator: new IamAuthenticator({
apikey: process.env.IBM_API_KEY
}),
serviceUrl: process.env.IBM_SERVICE_URL
})
const uuid = randomUUID()
const { echoTitle, chapterTitle, chapterText } = req.body
const synthesizeParams = {
text: chapterText,
accept: 'audio/mp3',
voice: 'en-US_KevinV3Voice'
}
textToSpeech
.synthesize(synthesizeParams)
.then(buffer => {
const s3Params = {
Bucket: 'waveforms/audioform',
Key: `${uuid}.mp3`,
Body: buffer.result,
ContentType: 'audio/mp3',
ACL: 'public-read'
}
console.log(buffer.result)
console.log(s3Params)
s3.upload(s3Params, function (s3Err, data) {
if (s3Err) throw s3Err
console.log(`File uploaded successfully at ${data.Location}`)
})
})
.catch(err => {
console.log('error:', err)
})
const dbs = await client.query(
q.Create(q.Collection('audioform'), {
data: {
title: echoTitle,
published: 2022,
leadAuthor: 'winter',
user: req.session.userId,
authors: 1,
playTime: 83,
chapters: 1,
gpt3Description: '',
likes: 20,
image:
'https://waveforms.s3.us-east-2.amazonaws.com/images/Mars.jpeg',
trackURL: `https://waveforms.s3.us-east-2.amazonaws.com/audioform/${uuid}.mp3`,
albumTracks: [
{
title: chapterTitle,
text: chapterText,
trackURL: `https://waveforms.s3.us-east-2.amazonaws.com/audioform/${uuid}.mp3`
}
]
}
})
)
res.status(200).json(dbs.data)
} catch (e) {
res.status(500).json({ error: e.message })
}
})
【问题讨论】:
-
您似乎没有在等待
textToSpeech.synthesize()结果,因此代码继续到await client.query(),然后返回响应。 -
@jarmod 代码应该是什么样的?是否只需要:
await textToSpeech.synthesize()?这是唯一必要的改变吗? -
@jarmod 我加了
await,但是还是一样的问题,S3桶里没有注册上传。 -
类似
const buffer = await textToSpeech.synthesize(synthesizeParams);,然后你可以在s3Params中使用缓冲区,然后你可以await s3.upload(s3Params).promise()。 -
我不完全确定那会是什么样子 - 我收到
await s3...的类型错误
标签: javascript node.js amazon-web-services amazon-s3 next.js