【问题标题】:How to force AWS Cognito: signUp() to execute synchronously (nodejs)如何强制 AWS Cognito:signUp() 同步执行(nodejs)
【发布时间】:2019-09-24 05:45:53
【问题描述】:

我正在尝试设置一个节点应用程序,该应用程序使用 AWS cognito sdk 来注册/登录/确认/验证用户。

我目前无法从 signUp() 方法获得响应,因为代码似乎是异步运行的。

我已经尝试定义一个异步函数 register_user(...) 并将所需的参数传递给一个单独的 register(...) 函数以等待 signUp 响应,然后继续进入 register_user(...)。

进口声明

const AmazonCognitoIdentity = require('amazon-cognito-identity-js');
const CognitoUserPool = AmazonCognitoIdentity.CognitoUserPool;
const AWS = require('aws-sdk');
const request = require('request');
const jwkToPem = require('jwk-to-pem');
const jwt = require('jsonwebtoken');
global.fetch = require('node-fetch');

注册函数

function register(userPool, email, password, attribute_list){

    let response;

    userPool.signUp(email, password, attribute_list, null, function(err, result){
        console.log("inside")
        if (err){
            console.log(err.message);
            response = err.message;
            return response;
        } 
        cognitoUser = result.user;
    });

    return "User succesfully registered."

}

注册用户

var register_user = async function(reg_payload){

    email = reg_payload['email']
    password = reg_payload['password']
    confirm_password = reg_payload['confirm_password']

    // define pool data
    var poolData = {
      UserPoolId : cognitoUserPoolId,
      ClientId : cognitoUserPoolClientId
    };

    var userPool = new AmazonCognitoIdentity.CognitoUserPool(poolData);

    var attribute_list = [];

    // define fields needed
    var dataEmail = {
        Name : 'email',
        Value : email
    };

    var attributeEmail = new AmazonCognitoIdentity.CognitoUserAttribute(dataEmail);

    attribute_list.push(attributeEmail);

    if (password === confirm_password){

        console.log("here")

        var result = await register(userPool, email, password, attribute_list);

        console.log(result)

        console.log("here2")

    } else {
        return "Passwords do not match."
    }
};

我发现即使我已经定义了指定要等待的注册函数,行为仍然是异步的。

有没有办法强制 signUp 方法在 register_user(...) 函数中同步运行?非常感谢。

【问题讨论】:

    标签: javascript node.js amazon-web-services async-await amazon-cognito


    【解决方案1】:

    如果你想在你的register_user 函数中 await 它,你需要改变你的 register 函数来返回一个 Promise

    function register(userPool, email, password, attribute_list) {
      return new Promise((resolve, reject) => {
        userPool.signUp(email, password, attribute_list, null, (err, result) => {
          console.log('inside');
          if (err) {
            console.log(err.message);
            reject(err);
            return;
          }
          cognitoUser = result.user;
          resolve(cognitoUser)
        });
      });
    }
    

    【讨论】:

    • 如果一切正常,您会在调用结果中获得 cognitoUser,但是如何跟踪其中的错误?
    【解决方案2】:

    不要忘记将await 放入try and catch like

     try {
            var result = await register(userPool, email, password, attribute_list);
    
            console.log(result);
        } catch (e) {
            console.error(e); // 30
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-29
      • 1970-01-01
      • 1970-01-01
      • 2018-07-09
      • 2018-02-09
      • 2011-11-26
      相关资源
      最近更新 更多