问题描述
我一直在尝试将 google translate API 与我的 redux 应用程序集成,但我似乎无法让它工作。我已经下载了我的私钥并将它们保存到本地文件路径。当我运行 example code 时,它运行良好。当我尝试在异步 redux thunk 中调用 translate
函数时出现问题,如下所示:
const projectId = '{my google cloud project id goes here}'
const keyFilename = '{the full path to my private key goes here}'
const translate = new Translate({projectId,keyFilename});
export const addPairs = createAsyncThunk(
'pairs/addPairs',async () => {
let [translations] = await translate.translate('hello world','en');
return translations
}
)
每当我使用 dispatch
调用此函数时,它总是返回此错误:
fs.createReadStream is not a function
我似乎无法弄清楚为什么会这样,我已经尝试搜索任何其他解决方案,但似乎之前没有人有过类似的用例......任何帮助都会非常感谢!
解决方法
我刚刚意识到我无法从浏览器上的本地文件系统读取密钥(因为 API 显然是用于服务器端代码的)所以最好的方法是设置一个本地服务器来提供服务API,然后从客户端访问它。糟糕。