【问题标题】:Nuxt application with amazon-cognito-identity-js package gives error fetch is not defined带有 amazon-cognito-identity-js 包的 Nuxt 应用程序给出错误未定义
【发布时间】:2019-12-23 17:20:36
【问题描述】:

我在 nuxt 应用程序中包含了 amazon-cognito-identity-js 包,我想使用此包提供的登录方法/注册方法。

但我收到以下错误

获取未定义

我安装了 node-fetch 并修改了 node_module/amazon-cognito-identity-js/Clinet.js 如下

const fetch = require('node-fetch');

这解决了我的问题,但我不想修改节点包。

【问题讨论】:

    标签: amazon-cognito nuxt.js


    【解决方案1】:

    我添加了以下代码。

    import { CognitoUserPool, CognitoUserAttribute } from 'amazon-cognito-identity-js'
    
    global.fetch = require('node-fetch')
    

    这解决了我的问题。

    【讨论】:

      【解决方案2】:

      documentation

      注意:此库使用 Fetch API。对于较旧的浏览器或在 Node.js,您可能需要包含一个 polyfill。例如。

      global.fetch = require('node-fetch');
      var AmazonCognitoIdentity = require('amazon-cognito-identity-js');
      

      【讨论】:

      • 您好,谢谢您的回复。我浏览了文档。但不确定我需要把这段代码放在哪里?
      • 没有。我直接在 nuxt 页面上使用 import。
      • import 只是 require 的替代品
      猜你喜欢
      • 2019-02-10
      • 1970-01-01
      • 2019-12-11
      • 2021-11-11
      • 2021-05-31
      • 2018-12-15
      • 2020-12-09
      • 2019-05-19
      • 1970-01-01
      相关资源
      最近更新 更多