首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >角2中的认知ListUsers函数

角2中的认知ListUsers函数
EN

Stack Overflow用户
提问于 2017-05-04 03:47:40
回答 2查看 3.3K关注 0票数 1

我试图在角2中使用listUsers函数,每次调用该函数时,我都会收到一个错误,说明listUsers不是一个函数。

我正在使用AWS认知快速启动的角度2 (https://github.com/awslabs/aws-cognito-angular2-quickstart)的例子,我正在添加到cognito.service.ts文件,试图合并listUsers的功能。

Javascript (http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/CognitoIdentityServiceProvider.html#listUsers-property)的文档要求调用该函数,如下所示:

代码语言:javascript
复制
var params = {
  UserPoolId: 'STRING_VALUE', /* required */
  AttributesToGet: [
    'STRING_VALUE',
    /* more items */
  ],
  Filter: 'STRING_VALUE',
  Limit: 0,
  PaginationToken: 'STRING_VALUE'
};
cognitoidentityserviceprovider.listUsers(params, function(err, data) {
  if (err) console.log(err, err.stack); // an error occurred
  else     console.log(data);           // successful response
});

我尝试在listUsers中使用cognito.service.ts函数,如下所示:

代码语言:javascript
复制
@Injectable()
export class CognitoUtil {

    public static _REGION = environment.region;
    public static _IDENTITY_POOL_ID = environment.identityPoolId;
    public static _USER_POOL_ID = environment.userPoolId;
    public static _CLIENT_ID = environment.clientId;

    public static _POOL_DATA = {
        UserPoolId: CognitoUtil._USER_POOL_ID,
        ClientId: CognitoUtil._CLIENT_ID
    };

    public static getAwsCognito(): any {
        return AWSCognito;
    }

    getUserPool() {
        return new AWSCognito.CognitoIdentityServiceProvider.CognitoUserPool(CognitoUtil._POOL_DATA);
    }

    getCurrentUser() {
        return this.getUserPool().getCurrentUser();
    }

    getList(myparams){
        var params = {
            UserPoolId: environment.userPoolId, /* required */
            AttributesToGet: [],
            Filter: "",
            Limit: 10
        };
        var cognito = new AWSCognito.CognitoIdentityServiceProvider.CognitoUserPool(CognitoUtil._POOL_DATA);
            cognito.listUsers(params, function(err, data) {
            if (err) console.log(err, err.stack);// an error occurred
            else return data;// successful response
        });
    }

    getCognitoIdentity(): string {
        return AWS.config.credentials.identityId;
    }

    getAccessToken(callback: Callback): void {
        if (callback == null) {
            throw("CognitoUtil: callback in getAccessToken is null...returning");
        }
        if (this.getCurrentUser() != null)
            this.getCurrentUser().getSession(function (err, session) {
                if (err) {
                    console.log("CognitoUtil: Can't set the credentials:" + err);
                    callback.callbackWithParam(null);
                }
                else {
                    if (session.isValid()) {
                    callback.callbackWithParam(session.getAccessToken().getJwtToken());
                    }
                }
            });
        else
            callback.callbackWithParam(null);
    }

    getIdToken(callback: Callback): void {
        if (callback == null) {
            throw("CognitoUtil: callback in getIdToken is null...returning");
        }
        if (this.getCurrentUser() != null)
            this.getCurrentUser().getSession(function (err, session) {
                if (err) {
                    console.log("CognitoUtil: Can't set the credentials:" + err);
                    callback.callbackWithParam(null);
                }
                else {
                    if (session.isValid()) {
                    callback.callbackWithParam(session.getIdToken().getJwtToken());
                    } else {
                        console.log("CognitoUtil: Got the id token, but the session isn't valid");
                    }
                }
            });
        else
            callback.callbackWithParam(null);
    }

    getRefreshToken(callback: Callback): void {
        if (callback == null) {
            throw("CognitoUtil: callback in getRefreshToken is null...returning");
        }
        if (this.getCurrentUser() != null)
            this.getCurrentUser().getSession(function (err, session) {
                if (err) {
                    console.log("CognitoUtil: Can't set the credentials:" + err);
                    callback.callbackWithParam(null);
                }

                else {
                    if (session.isValid()) {
                        callback.callbackWithParam(session.getRefreshToken());
                    }
                }
            });
        else
            callback.callbackWithParam(null);
    }

    refresh(): void {
        this.getCurrentUser().getSession(function (err, session) {
            if (err) {
                console.log("CognitoUtil: Can't set the credentials:" + err);
            }

            else {
                if (session.isValid()) {
                    console.log("CognitoUtil: refreshed successfully");
                } else {
                    console.log("CognitoUtil: refreshed but session is still not valid");
                }
            }
        });
    }
}

我就是这样在组件中调用getList(params)的:

代码语言:javascript
复制
constructor(public cognitoUtil: CognitoUtil){}
viewUsers(){
    console.log(this.cognitoUtil.getList(this.param));
}

我尝试了多次不同的事情来使这个工作,我似乎无法使它工作。我尝试了ListUsers而不是listUsers,并且尝试调用getUserPool()而不是使用listUsers

更新

我按照建议把AWSCognito.CognitoIdentityServiceProvider.CognitoUserPool改成了AWS.CognitoIdentityServiceProvider。我现在收到错误:

代码语言:javascript
复制
ConfigError: Missing region in config
Stack trace:
_xamzrequire<[33]</n.EventListeners.Core</<@http://localhost:3000/assets/js/aws-sdk-2.7.13.min.js:4:11200
_xamzrequire<[58]</n.SequentialExecutor<.callListeners@http://localhost:3000/assets/js/aws-sdk-2.7.13.min.js:4:21879
o@http://localhost:3000/assets/js/aws-sdk-2.7.13.min.js:4:21745
_xamzrequire<[33]</n.EventListeners.Core</</<@http://localhost:3000/assets/js/aws-sdk-2.7.13.min.js:4:11061
e@http://localhost:3000/assets/js/aws-sdk-2.7.13.min.js:5:24062
_xamzrequire<[20]</n.Config<.getCredentials@http://localhost:3000/assets/js/aws-sdk-2.7.13.min.js:5:24648
_xamzrequire<[33]</n.EventListeners.Core</<@http://localhost:3000/assets/js/aws-sdk-2.7.13.min.js:4:10913
_xamzrequire<[58]</n.SequentialExecutor<.callListeners@http://localhost:3000/assets/js/aws-sdk-2.7.13.min.js:4:21850
_xamzrequire<[58]</n.SequentialExecutor<.emit@http://localhost:3000/assets/js/aws-sdk-2.7.13.min.js:4:21585
_xamzrequire<[54]</</n.Request<.emitEvent@http://localhost:3000/assets/js/aws-sdk-2.7.13.min.js:3:9935
_xamzrequire<[54]</</c.setupStates/t@http://localhost:3000/assets/js/aws-sdk-2.7.13.min.js:3:5785
_xamzrequire<[82]</n.prototype.runTo@http://localhost:3000/assets/js/aws-sdk-2.7.13.min.js:4:1187
_xamzrequire<[54]</</n.Request<.runTo@http://localhost:3000/assets/js/aws-sdk-2.7.13.min.js:3:7345
_xamzrequire<[54]</</n.Request<.send@http://localhost:3000/assets/js/aws-sdk-2.7.13.min.js:3:7243
_xamzrequire<[59]</n.Service<.makeRequest@http://localhost:3000/assets/js/aws-sdk-2.7.13.min.js:2:27369
_xamzrequire<[59]</<.defineMethods/</t.prototype[e]@http://localhost:3000/assets/js/aws-sdk-2.7.13.min.js:2:30572
CognitoUtil.prototype.getList@http://localhost:3000/main.bundle.js:436:10
organizationsViewComponent.prototype.viewUsersInOrg@http://localhost:3000/main.bundle.js:1435:22
anonymous/_View_organizationsViewComponent1.prototype._handle_click_13_0@organizationsViewComponent.ngfactory.js:1991:15
[2]/</</DebugAppView</DebugAppView.prototype.eventHandler/<@http://localhost:3000/vendor.bundle.js:9712:29
decoratePreventDefault/<@http://localhost:3000/vendor.bundle.js:11832:41
[3]/</</DomEventsPlugin</DomEventsPlugin.prototype.addEventListener/outsideHandler/<@http://localhost:3000/vendor.bundle.js:11945:116
ZoneDelegate.prototype.invoke@http://localhost:3000/polyfills.bundle.js:14768:18
NgZoneImpl/this.inner<.onInvoke@http://localhost:3000/vendor.bundle.js:6256:33
ZoneDelegate.prototype.invoke@http://localhost:3000/polyfills.bundle.js:14767:18
Zone.prototype.runGuarded@http://localhost:3000/polyfills.bundle.js:14664:29
[2]/</</NgZoneImpl</NgZoneImpl.prototype.runInnerGuarded@http://localhost:3000/vendor.bundle.js:6285:72
[2]/</</NgZone</NgZone.prototype.runGuarded@http://localhost:3000/vendor.bundle.js:6518:63
[3]/</</DomEventsPlugin</DomEventsPlugin.prototype.addEventListener/outsideHandler@http://localhost:3000/vendor.bundle.js:11945:79
ZoneDelegate.prototype.invokeTask@http://localhost:3000/polyfills.bundle.js:14801:22
Zone.prototype.runTask@http://localhost:3000/polyfills.bundle.js:14690:29
ZoneTask/this.invoke@http://localhost:3000/polyfills.bundle.js:14871:29
 _xamzrequire<[33]</n.EventListeners.Core</<@http://localhost:3000/assets/js/aws-sdk-2.7.13.min.js:4:11200
_xamzrequire<[58]</n.SequentialExecutor<.callListeners@http://localhost:3000/assets/js/aws-sdk-2.7.13.min.js:4:21879
o@http://localhost:3000/assets/js/aws-sdk-2.7.13.min.js:4:21745
_xamzrequire<[33]</n.EventListeners.Core</</<@http://localhost:3000/assets/js/aws-sdk-2.7.13.min.js:4:11061
e@http://localhost:3000/assets/js/aws-sdk-2.7.13.min.js:5:24062
_xamzrequire<[20]</n.Config<.getCredentials@http://localhost:3000/assets/js/aws-sdk-2.7.13.min.js:5:24648
_xamzrequire<[33]</n.EventListeners.Core</<@http://localhost:3000/assets/js/aws-sdk-2.7.13.min.js:4:10913
_xamzrequire<[58]</n.SequentialExecutor<.callListeners@http://localhost:3000/assets/js/aws-sdk-2.7.13.min.js:4:21850
_xamzrequire<[58]</n.SequentialExecutor<.emit@http://localhost:3000/assets/js/aws-sdk-2.7.13.min.js:4:21585
_xamzrequire<[54]</</n.Request<.emitEvent@http://localhost:3000/assets/js/aws-sdk-2.7.13.min.js:3:9935
_xamzrequire<[54]</</c.setupStates/t@http://localhost:3000/assets/js/aws-sdk-2.7.13.min.js:3:5785
_xamzrequire<[82]</n.prototype.runTo@http://localhost:3000/assets/js/aws-sdk-2.7.13.min.js:4:1187
_xamzrequire<[54]</</n.Request<.runTo@http://localhost:3000/assets/js/aws-sdk-2.7.13.min.js:3:7345
_xamzrequire<[54]</</n.Request<.send@http://localhost:3000/assets/js/aws-sdk-2.7.13.min.js:3:7243
_xamzrequire<[59]</n.Service<.makeRequest@http://localhost:3000/assets/js/aws-sdk-2.7.13.min.js:2:27369
_xamzrequire<[59]</<.defineMethods/</t.prototype[e]@http://localhost:3000/assets/js/aws-sdk-2.7.13.min.js:2:30572
CognitoUtil.prototype.getList@http://localhost:3000/main.bundle.js:436:10
organizationsViewComponent.prototype.viewUsers@http://localhost:3000/main.bundle.js:1435:22
anonymous/_View_organizationsViewComponent1.prototype._handle_click_13_0@organizationsViewComponent.ngfactory.js:1991:15
[2]/</</DebugAppView</DebugAppView.prototype.eventHandler/<@http://localhost:3000/vendor.bundle.js:9712:29
decoratePreventDefault/<@http://localhost:3000/vendor.bundle.js:11832:41
[3]/</</DomEventsPlugin</DomEventsPlugin.prototype.addEventListener/outsideHandler/<@http://localhost:3000/vendor.bundle.js:11945:116
ZoneDelegate.prototype.invoke@http://localhost:3000/polyfills.bundle.js:14768:18
NgZoneImpl/this.inner<.onInvoke@http://localhost:3000/vendor.bundle.js:6256:33
ZoneDelegate.prototype.invoke@http://localhost:3000/polyfills.bundle.js:14767:18
Zone.prototype.runGuarded@http://localhost:3000/polyfills.bundle.js:14664:29
[2]/</</NgZoneImpl</NgZoneImpl.prototype.runInnerGuarded@http://localhost:3000/vendor.bundle.js:6285:72
[2]/</</NgZone</NgZone.prototype.runGuarded@http://localhost:3000/vendor.bundle.js:6518:63
[3]/</</DomEventsPlugin</DomEventsPlugin.prototype.addEventListener/outsideHandler@http://localhost:3000/vendor.bundle.js:11945:79
ZoneDelegate.prototype.invokeTask@http://localhost:3000/polyfills.bundle.js:14801:22
Zone.prototype.runTask@http://localhost:3000/polyfills.bundle.js:14690:29
ZoneTask/this.invoke@http://localhost:3000/polyfills.bundle.js:14871:29
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-05-04 18:08:13

listUsers操作是通过主AWS公开的,它是一个经过身份验证的操作,这意味着它需要存在AWS凭据才能运行。节点中的代码:

代码语言:javascript
复制
var aws = require('aws-sdk');
aws.config.update({accessKeyId: 'akid', secretAccessKey: 'secret'});

var CognitoIdentityServiceProvider = aws.CognitoIdentityServiceProvider;
var client = new CognitoIdentityServiceProvider({ apiVersion: '2016-04-19', region: 'us-east-1' });

//now you can call listUsers on the client object     

请注意,可能有不同的方法可以配置AWS凭据来调用操作。您确实需要凭据,因为这是一个经过身份验证的操作。CreateUserPool类似,您只需要在调用中以JSON的形式传递适当的参数。

票数 3
EN

Stack Overflow用户

发布于 2017-05-04 05:02:16

您正在对一个类型为:AWSCognito.CognitoIdentityServiceProvider.CognitoUserPool的对象调用AWSCognito.CognitoIdentityServiceProvider.CognitoUserPool,需要对一个类型的对象调用它:AWS.CognitoIdentityServiceProvider CognitoUserPool没有这样的方法。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43773553

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档