【问题标题】:How can I use the Google Translate API inside a redux async thunk?如何在 redux 异步 thunk 中使用 Google Translate API?
【发布时间】:2021-04-29 22:06:22
【问题描述】:

我一直在尝试将谷歌翻译 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

我似乎无法弄清楚为什么会这样,我已经尝试搜索任何其他解决方案,但似乎没有人以前有过类似的用例......任何帮助都是非常感谢!

【问题讨论】:

    标签: javascript npm google-cloud-platform google-translate google-translation-api


    【解决方案1】:

    我刚刚意识到我无法从浏览器上的本地文件系统中读取密钥(因为 API 显然是为服务器端代码设计的)所以最好的方法是设置一个本地服务器来提供服务API,然后从客户端访问它。哎呀。

    【讨论】:

      猜你喜欢
      • 2020-10-28
      • 2021-12-11
      • 2021-06-20
      • 2021-10-04
      • 2018-01-31
      • 2017-07-28
      • 2017-06-15
      • 2017-03-03
      • 2018-01-10
      相关资源
      最近更新 更多