如果我添加到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);
});
}
您似乎混淆了两种类型的云功能:
将函数声明为functions.https.onRequest
时,需要将响应写入响应对象。基于documentation on calling functions through HTTP requests,你需要做:
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);
});
});
如果你想要call your Cloud Function from within your app using the Firebase SDK,你需要将你的函数声明为:
exports.listAllUsers = functions.https.onCall((data, context) => {
// List all users
return listAllUsers();
});