I'm creating all users from Firebase console, Now I wanted copy all users to realtime db under users
node.
Below is my Google cloud function
exports.getAllUsers = functions.https.onRequest((req, res) => {
cors(req, res, () => {
app.auth().listUsers(1000)
.then((listUsersResult) => {
var db = admin.database();
var ref = db.ref("users");
const usersRef = ref.child('users');
usersRef.set(listUsersResult, { merge: true } );
ref.once("value", function (snapshot) {
console.log(snapshot.val());
});
// admin.database().ref('users').set(listUsersResult, { merge: true })
res.send(listUsersResult);
})
.catch(function (error) {
console.log('Oh no! Firebase listUsers Error:', error);
});
});
});
By using firebase-admin
, I could get all the users list but When I tried to update the DB i'm getting the below error
Oh no! Firebase listUsers Error: Error: Reference.set failed: onComplete argument must be a valid function.
How can I fix this?
CodePudding user response:
You're passing { merge: true }
to the call to set
, but that option only exists in the Firestore API - not in the API of the Realtime Database. So:
usersRef.set(listUsersResult);
In addition, you need to handle the asynchronous nature of set
and once
as Renaud commented. So combining the above with that gets to:
usersRef.set(listUsersResult)
.then(() => {
ref.once("value", function (snapshot) {
console.log(snapshot.val());
res.send(listUsersResult);
});
});
That leaves the fact that your usersRef
refers to /users/users
in the database, which you'll probably also want to fix.