【问题标题】:AWS cognito: adminUpdateUserAttributes not working and not giving an error , am i missing something?AWS cognito:adminUpdateUserAttributes 不起作用并且没有给出错误,我错过了什么吗?
【发布时间】:2019-09-26 12:53:04
【问题描述】:

我无法让 Cognito 工作的 adminUpdateUserAttributes。 cli 有效,我可以让用户添加/更改它们,但希望看到它正常工作。

我在 lambda 函数上使用 AmazonCognitoPowerUser 一个 AWS 托管策略,并且 lambda 正在触发,有什么我想念的东西吗?这听起来很简单,但它只是不起作用。

还有一种方法可以在不自己创建的情况下获取默认的创建日期。

const AWS = require('aws-sdk');
const cognitoidentityserviceprovider =  new AWS.CognitoIdentityServiceProvider();

exports.handler = async (event) => {
    cognitoidentityserviceprovider.adminUpdateUserAttributes(
  {
    UserAttributes: [
      {
        Name: 'custom:Date_Created',
        Value: new Date().toString() 
      }
      ....
    ],
    UserPoolId: " the correctpool id",
    Username: "dagTest"
  },
  function(err, data) { 
     if (err) console.log(err, err.stack); // an error occurred
  else     console.log(data);        
}
)};

// no errors and returns nothing as it says it should.

【问题讨论】:

    标签: node.js aws-sdk amazon-cognito


    【解决方案1】:

    我猜这是因为您没有等待结果,并且 lambda 在调用 adminUpdateUserAttributes() 之后终止并且 dows 没有等到它返回。

    我建议您更改为基于 promise 的调用并执行 try/catch

    exports.handler = async (event) => {
      try{
        // no callback here
        const data = await cognitoidentityserviceprovider
          .adminUpdateUserAttributes(attributes)
          .promise()
        console.log('success', data)
      } catch(error) {
        console.error('error', error)
      }
    )}
    

    【讨论】:

    • 这个答案为我节省了很多时间。谢谢你提供它。您是否知道为什么需要“promise()”行?我已经测试并发现它确实是必需的。但为什么? 'try/catch' + 'await' 不应该足以确保 Lambda 在 updateUserAttributes 函数运行之前不会完成吗?
    • 我不完全确定,但 IIRC SDK 的 API 需要回调。如果你不调用promise,结果将不会被消耗。因此,校准承诺的需要是 AWS 开发工具包开发人员的设计决策。
    • 谢谢。这很有帮助。
    猜你喜欢
    • 1970-01-01
    • 2022-10-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多