Node.js 从KeyClope获取用户名列表,并将其保存到数组中以供以后使用
我正在尝试编写代码来检索存在于KeyClope用户列表中的所有用户。我正在获取KeyClope中的所有用户。但我只想从完整的用户列表中获取用户名或ID,并将该值存储在一个数组中,我可以使用该数组将角色分配给用户 myfile.csNode.js 从KeyClope获取用户名列表,并将其保存到数组中以供以后使用,node.js,keycloak,httpresponse,restapi,keycloak-rest-api,Node.js,Keycloak,Httpresponse,Restapi,Keycloak Rest Api,我正在尝试编写代码来检索存在于KeyClope用户列表中的所有用户。我正在获取KeyClope中的所有用户。但我只想从完整的用户列表中获取用户名或ID,并将该值存储在一个数组中,我可以使用该数组将角色分配给用户 myfile.cs let userName = [] function GetUser(userName,kc_accessToken) { let url = `${path}/users`; return axios_instance.get(url,
let userName = []
function GetUser(userName,kc_accessToken) {
let url = `${path}/users`;
return axios_instance.get(url,
{
headers: {
"content-type": "application/json",
"authorization": `Bearer ${kc_accessToken}`
}
}).then(function (response) {
console.log("User names!!");
//full list (working)
//userName = response.data
//console.log(response.data);
//only usernames or ids (not working)
userName = response.data.username
console.log(response.data.username);
})
.catch(function (error) {
console.log("No Users");
});
}
函数调用
http.createServer(function Test() {
getAccessToken().then(function (response) {
kc_accessToken = response.data.access_token;
GetUser(userName,kc_accessToken).then((resp) => {
})
}).catch(function (error) {
// handle error
console.log(error);
})
.then(function () {
// always executed
});;
}).listen(8081);
尝试列出所有用户时的输出
当我试图只获取用户的用户名时,我的输出
您可以在
response.data
中获得一个用户对象数组,因此您可以简单地应用map函数生成一个包含用户名的字符串数组:
const usernames=response.data.map(user=>user.username)
哇,很好用,非常感谢。祝你有美好的一天