如果我添加到Firebase云功能,则无法获得用户列表。我尝试了很多事情,并遵循了Firebase文档上的指南,但是它一直运行,但从未加载。
exports.listAllUsers = functions.https.onRequest((data, context) => {
// List all users
return listAllUsers();
});
function listAllUsers() {
// List batch of users, 1000 at a time.
var allUsers = [];
return admin.auth().listUsers()
.then(function (listUsersResult) {
listUsersResult.users.forEach(function (userRecord) {
// For each user
var userData = userRecord.toJSON();
allUsers.push(userData);
});
return allUsers
})
.catch(function (error) {
console.log("Error listing users:", error);
});
}
答案 0 :(得分:3)
您似乎混淆了两种类型的Cloud函数:
在将函数声明为functions.https.onRequest
时,您需要将响应写入响应对象。根据{{3}},您需要执行以下操作:
exports.listAllUsers = functions.https.onRequest((req, res) => {
// List batch of users, 1000 at a time.
var allUsers = [];
return admin.auth().listUsers()
.then(function (listUsersResult) {
listUsersResult.users.forEach(function (userRecord) {
// For each user
var userData = userRecord.toJSON();
allUsers.push(userData);
});
res.status(200).send(JSON.stringify(allUsers));
})
.catch(function (error) {
console.log("Error listing users:", error);
res.status(500).send(error);
});
});
如果您想documentation on calling functions through HTTP requests,则需要将函数声明为:
exports.listAllUsers = functions.https.onCall((data, context) => {
// List all users
return listAllUsers();
});