我想在Lambda函数中使用Cognito注册用户。但是我收到“ TypeError:提取不是函数”
我的代码基本上是this中的第3步。但是,即使安装了node-fetch,我仍然收到上述错误。据我了解,Cognito SDK利用了fetch。那么这根本不可能吗?我需要启动节点服务器吗?
const AWS = require("aws-sdk");
const AmazonCognitoIdentity = require("amazon-cognito-identity-js");
//Configuring pool data of Cognito Identity Pool
const poolData = {
UserPoolId: "us-east-2_aCvZbFzeS",
ClientId: "4nv2krchr77pbrq3cpk0q0kknu"
};
const userPool = new AmazonCognitoIdentity.CognitoUserPool(poolData);
AWS.config.region = "us-east-2";
const attributeList = [];
attributeList.push(
new AmazonCognitoIdentity.CognitoUserAttribute({
Name: "email",
Value: "sampleEmail@gmail.com"
})
);
userPool.signUp(
"sampleEmail@gmail.com",
"SamplePassword123",
attributeList,
null,
function(err, result) {
if (err) {
console.log(err);
return;
}
const cognitoUser = result.user;
console.log("user name is " + cognitoUser.getUsername());
}
);
const data = JSON.parse(event.body);
const headers = {
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Credentials": true
};
const response = {
statusCode: 200,
headers: headers,
"Content-Type": "application/json",
body: JSON.stringify(data.age)
};
callback(null, response);
};
//I keep receiving this error when attempting to hit the endpoint with Postman:
"errorMessage": "Uncaught error in your 'hello' handler",
"errorType": "TypeError",
"stackTrace": [
"TypeError: fetch is not a function"
答案 0 :(得分:0)
您绝对可以使用Lambda的Cognito!来源:已经做到了。
不过,您可能无法很好地使用Lambda的AWS Cognito JS SDK。
AWS Cognito JS SDK似乎是为内置fetch
的客户端应用程序设计的。您已经安装了node-fetch
,但SDK并未加载它,因为它认为它不需要内置,因为它认为它不需要内置。
我看到两个选择:
node-fetch
或使其在服务器端正常运行之前,在本地破解AWS Cognito JS SDK。 This thread很好地描述了同一问题和一些变通方法。 best one for you可能是:
global.fetch = require('node-fetch')
const AmazonCognitoIdentity = require('amazon-cognito-identity-js');
在您的脚本中,这应该使它显示为SDK代码的内置代码,而不会破坏内部结构。