Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/azure/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript AdminListGroupsForUser返回的用户不存在_Javascript_Aws Sdk_Amazon Cognito - Fatal编程技术网

Javascript AdminListGroupsForUser返回的用户不存在

Javascript AdminListGroupsForUser返回的用户不存在,javascript,aws-sdk,amazon-cognito,Javascript,Aws Sdk,Amazon Cognito,我的应用程序是一个使用2个服务进行身份验证的过程,其中一个是AWS Cognito。当用户经过身份验证时(Cognito提供的JWT令牌也是如此),我尝试通过AdminListGroupsForUserAPI调用列出该用户的组。我得到了UserNotFoundException,这很奇怪,因为上一次调用刚刚用相同的凭据验证了该用户 我尝试了以下几点: router.post("/groups/list", (req, res, next) => { const { email, l

我的应用程序是一个使用2个服务进行身份验证的过程,其中一个是AWS Cognito。当用户经过身份验证时(Cognito提供的JWT令牌也是如此),我尝试通过
AdminListGroupsForUser
API调用列出该用户的组。我得到了UserNotFoundException,这很奇怪,因为上一次调用刚刚用相同的凭据验证了该用户

我尝试了以下几点:

router.post("/groups/list", (req, res, next) => {
    const { email, limit, nextToken } = req.body;
    const listGroupsForUserParams = getAdminListGroupsForUserParams({
        email,
        limit,
        nextToken
    });
    const getUserParams = getAdminGetUserParams(email);

    cognitoClient.adminListGroupsForUser(listGroupsForUserParams, (listErr, listData) => {
        cognitoClient.adminGetUser(getUserParams, (getErr, getData) => {
            console.log(listErr);          // "UserNotFoundException"
            console.log(listData);         // null
            console.log(getErr);           // null
            console.log(getData);          // User
        });
    });
});
listgroupsforserparams
getUserParams
包含相同的信息,即:

{
    UserPoolId: "...",
    Username: "test@example.com"     // I use email as Username
}
我不明白为什么前一个调用在池中找不到用户,而后一个可以

见(参考文献):


我也有同样的问题,出于某种原因,
adminListGroupsForUser
函数不接受电子邮件作为用户名,而
adminGetUser
则接受。 我通过使用
adminGetUser
检索用户数据来解决这个问题。它返回用户及其所有属性。检索名为
sub
的属性值,并将其用作
adminListGroupsForUser
调用的用户名

大概是这样的:

const getParams = {
    UserPoolId: "" /*put your user pool Id here*/,
    Username: "" /* email */
};

cognitoidentityserviceprovider.adminGetUser(getParams, function(err, data) {
    if (err) {
      console.log(err, err.stack); // an error occurred
      return;
    }

    var sub;
    if (data.UserAttributes && data.UserAttributes.length) {
      for (var i = 0; i < data.UserAttributes.length; ++i) {
        const attr = data.UserAttributes[i];
        if (attr.Name === 'sub') {
          console.log(attr);
          sub = attr.Value
          break;
        }
      }
    }

    if (!sub)
        return;

    const groupsParams = {
      UserPoolId: event.userPoolId,
      Username: sub
    };  
    cognitoidentityserviceprovider.adminListGroupsForUser(groupsParams, function(err, data) {
        /* Your code using groups here */
    });
});
const getParams={
UserPoolId:“/*将您的用户池Id放在此处*/,
用户名:“/*电子邮件*/
};
cognitoidentityserviceprovider.adminGetUser(getParams,函数(err,数据){
如果(错误){
console.log(err,err.stack);//发生错误
返回;
}
var-sub;
if(data.UserAttributes&&data.UserAttributes.length){
对于(var i=0;i
您的cognitoClient的区域设置正确吗?@Deepthi是的,区域设置正确。我想如果不是这样,即使第二次调用
adminGetUser
也会失败