kolserdav
kolserdav

Reputation: 286

AWS Cognito user pool signup "Unknown error, the response body from fetch is: undefined"

When a user is registered, the user is added but the script returns an error object: {code: "UnknownError", message: "Unknown error, the response body from fetch is: undefined"}

I followed the recommendations from https://stackoverflow.com/a/52033216/8111346. But the problem is not solved.

index.html

 <!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<script src="dist/ww.js"></script>
<script>
  var global = global || window;
  var Buffer = Buffer || [];
  var process = process || {
      env: { DEBUG: undefined },
      version: []
    };
  var w = window.w();
  global.window = {w};
  global.navigator = () => null;
</script>
<script src="./dist/m.js"></script>
</body>
</html>

ww.js

const WindowMock = require('window-mock');
  import fetch from 'node-fetch'
  global.fetch = global.fetch || fetch
  function w(){
    return WindowMock.localStorage;
  }
  window.w = w;

m.js

import { CognitoUserPool, CognitoUserAttribute } from 'amazon-cognito-identity-js';

var poolData = { UserPoolId : "us-west-2_someData",
  ClientId : "someData"
};
var userPool = new CognitoUserPool(poolData);
var userName = "[email protected]";
var attributeList = [];

var dataEmail = {
  Name : 'email',
  Value : userName
};

var attributeEmail = new CognitoUserAttribute(dataEmail);


attributeList.push(attributeEmail);


userPool.signUp(userName, 'password', attributeList, null, function(err, result){
  if (err) {
    console.log(err);
    return;
  }
  cognitoUser = result.user;
  console.log('user name is ' + cognitoUser.getUsername());
});

webpack.config.js

const path = require('path');
module.exports = {
  entry: ["./module.js"],
    mode: 'development',
  output: {
    path: path.resolve(__dirname + '/dist'),
    filename: 'm.js'
  },
  module: {
    rules: [
      {
        test: /\.json$/,
        loader: 'json-loader'
      }
    ]
  },
     watch : true
};

In request to cognito-idp.us-west-2.amazonaws.com the header X-Amz-User-Agent: aws-amplify/0.1.x js is passed, but i don't use Amplify. Maybe this is due to the settings of my environment?

I am pleased to read your thoughts.

Upvotes: 0

Views: 1513

Answers (2)

Asaf Shveki
Asaf Shveki

Reputation: 736

Adding var before cognitoUser solved my problem:

var cognitoUser = result.user;

Upvotes: 1

Aldarund
Aldarund

Reputation: 17621

There is no fetch on node. You need to use polyfill for fetch e.g. node-fetch

import fetch from 'node-fetch'
global.fetch = global.fetch || fetch

But keep in mind that amplify not designed to work on node, so there could be other problems as well.

Upvotes: 1

Related Questions