未定义“ AWSCognito” [英] 'AWSCognito' is not defined
问题描述
我已经使用以下示例在nodeJS中实现AWS Cognito,但是我遇到了错误
'AWSCognito'未定义为no-undef
I have used the following example to implement AWS Cognito in nodeJS but i am facing the error "'AWSCognito' is not defined no-undef"
参考链接: https://github.com/aws/amazon-cognito-identity- js /
以下是我在App.js文件中的代码。我使用了react-app。
Following is my code in App.js file. I have used react-app.
import React, { Component } from 'react';
import logo from './logo.svg';
import './App.css';
import { CognitoUserPool, CognitoUserAttribute, CognitoUser } from 'amazon-cognito-identity-js';
class App extends Component {
constructor(props, context){
super(props, context);
this.state = {
items: []
}
}
render() {
const {items} = this.state;
return (
<div className="App">
<div className="App-header">
<img src={logo} className="App-logo" alt="logo" />
<h2>Welcome to React</h2>
</div>
<p className="App-intro">
To get started, edit <code>src/App.js</code> and save to reload.
</p>
<button onClick={(e) => this.doRegister(e)}>Register</button>
{ items.map(item => <p>{item.id}</p>) }
</div>
);
}
doRegister(){
console.log("Register User");
var poolData = {
UserPoolId : 'xxxxxxxxxxx', // Your user pool id here
ClientId : 'xxxxxxxxxxxxxxxxxx' // Your client id here
};
var userPool = new AWSCognito.CognitoIdentityServiceProvider.CognitoUserPool(poolData);
console.debug(userPool);
}
}
export default App;
这是package.json中的代码。您会看到aws-cognito-identity库已经添加。
This is the code in package.json. You can see the aws-cognito-identity library is already added.
{
"name": "my-app",
"version": "0.1.0",
"private": true,
"dependencies": {
"amazon-cognito-identity-js": "^1.19.0",
"react": "^15.6.1",
"react-dom": "^15.6.1",
"react-scripts": "1.0.10"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test --env=jsdom",
"eject": "react-scripts eject"
},
"devDependencies": {
"json-loader": "^0.5.4",
"webpack": "^3.0.0"
}
}
为什么显示AWSCognito是未定义的。我的代码中是否缺少与AWS Cognito相关的任何东西?
Why it is showing AWSCognito is undefined. Is there anything missing in my code related to AWS Cognito?
推荐答案
您需要适用于JavaScript的AWS开发工具包除了适用于JavaScript的Amazon Cognito身份SDK 。
npm install aws-sdk --save
然后,您可以将其导入为:
Then, you can import it as:
import * as AmazonCognitoIdentity from 'amazon-cognito-identity-js';
class App extends Component {
...
const poolData = { UserPoolId: 'YOUR_USER_POOL_ID', ClientId: 'YOUR_CLIENT_ID' };
const userPool = new AmazonCognitoIdentity.CognitoUserPool(poolData);
...
}
这篇关于未定义“ AWSCognito”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!