在 AWS lambda 上列出 cognito userpool 用户
Listing cognito userpool users on AWS lambda
我试图在我的 lambda 函数中列出我所有的认知用户,但是我在 return 中什么也没有得到,就好像回调没有被执行一样。我做错了什么?
下面代码的输出只是在控制台中向我打招呼。
var AWS = require("aws-sdk");
const cognitoidentityserviceprovider = new AWS.CognitoIdentityServiceProvider();
export async function main() {
console.log("hello")
var params = {
UserPoolId: "myuserpoolid",
AttributesToGet: ["username"]
};
cognitoidentityserviceprovider.listUsers(params, (err, data) => {
if (err) {
console.log(err, err.stack);
return err;
} else {
console.log(data);
return data;
}
});
}
首先,代码的结构是错误的。 Lambda 函数的头部应该有一定的结构,要么使用异步函数,要么使用非异步函数。由于您在示例中使用的是非异步代码,因此我将向您展示如何执行后续操作。
var AWS = require("aws-sdk");
const cognitoidentityserviceprovider = new AWS.CognitoIdentityServiceProvider();
exports.handler = function(event, context, callback) {
console.log("hello")
var params = {
UserPoolId: "myuserpoolid",
AttributesToGet: ["username"]
};
cognitoidentityserviceprovider.listUsers(params, (err, data) => {
if (err) {
console.log(err, err.stack);
callback(err) // here is the error return
} else {
console.log(data);
callback(null, data) // here is the success return
}
});
}
在这种情况下,Lambda 将仅在调用 callback
时(或超时时)完成。
同样,您可以使用异步函数,但您需要相应地重构代码。这是取自 official docs 的示例。请注意如何使用 promise 包装器。
const https = require('https')
let url = "https://docs.aws.amazon.com/lambda/latest/dg/welcome.html"
exports.handler = async function(event) {
const promise = new Promise(function(resolve, reject) {
https.get(url, (res) => {
resolve(res.statusCode)
}).on('error', (e) => {
reject(Error(e))
})
})
return promise
}
对于 AttributesToGet,不要使用用户名,因为它是始终返回的字段之一。以下是Attributes数组的成员,可用于AttributesToGet字段:
子,email_verified,phone_number_verified,phone_number,电子邮件。
例如
AttributesToGet: ["email","email_verified"]
我试图在我的 lambda 函数中列出我所有的认知用户,但是我在 return 中什么也没有得到,就好像回调没有被执行一样。我做错了什么?
下面代码的输出只是在控制台中向我打招呼。
var AWS = require("aws-sdk");
const cognitoidentityserviceprovider = new AWS.CognitoIdentityServiceProvider();
export async function main() {
console.log("hello")
var params = {
UserPoolId: "myuserpoolid",
AttributesToGet: ["username"]
};
cognitoidentityserviceprovider.listUsers(params, (err, data) => {
if (err) {
console.log(err, err.stack);
return err;
} else {
console.log(data);
return data;
}
});
}
首先,代码的结构是错误的。 Lambda 函数的头部应该有一定的结构,要么使用异步函数,要么使用非异步函数。由于您在示例中使用的是非异步代码,因此我将向您展示如何执行后续操作。
var AWS = require("aws-sdk");
const cognitoidentityserviceprovider = new AWS.CognitoIdentityServiceProvider();
exports.handler = function(event, context, callback) {
console.log("hello")
var params = {
UserPoolId: "myuserpoolid",
AttributesToGet: ["username"]
};
cognitoidentityserviceprovider.listUsers(params, (err, data) => {
if (err) {
console.log(err, err.stack);
callback(err) // here is the error return
} else {
console.log(data);
callback(null, data) // here is the success return
}
});
}
在这种情况下,Lambda 将仅在调用 callback
时(或超时时)完成。
同样,您可以使用异步函数,但您需要相应地重构代码。这是取自 official docs 的示例。请注意如何使用 promise 包装器。
const https = require('https')
let url = "https://docs.aws.amazon.com/lambda/latest/dg/welcome.html"
exports.handler = async function(event) {
const promise = new Promise(function(resolve, reject) {
https.get(url, (res) => {
resolve(res.statusCode)
}).on('error', (e) => {
reject(Error(e))
})
})
return promise
}
对于 AttributesToGet,不要使用用户名,因为它是始终返回的字段之一。以下是Attributes数组的成员,可用于AttributesToGet字段:
子,email_verified,phone_number_verified,phone_number,电子邮件。
例如
AttributesToGet: ["email","email_verified"]