0
votes

I would like to copy the email associated to each user to a Firestore collection ('users') using Cloud Functions. Each document in the collection has the UID of the user as its name.. I have the following function:

const getAllUsers = (req, res) => {

  auth.listUsers().then((userRecords) => {
    userRecords.users.forEach(
        (user) => db.collection('users').doc(user.uid).update({ "email": user.email }) 
        ) 

    res.end('Retrieved users list successfully.');
  }).catch((error) => console.log(error));
};

module.exports = {
  api: functions.https.onRequest(getAllUsers),
};

I am getting the following error for invalid data:

FirebaseError: Function DocumentReference.update() called with invalid data. Unsupported field value: undefined (found in field email)

Any ideas?

1

1 Answers

1
votes

Got this to work by converting the data to JSON beforehand, here is the working function:

const getAllUsers = (req, res) => {

  auth.listUsers().then((userRecords) => {
    userRecords.users.forEach(
        (user) => 
        function() {
                    let thisUser = user.toJSON();
        db.collection('users').doc(thisUser.uid).update({ "email": thisUser.email }) 
        }
        ) 

    res.end('Retrieved users list successfully.');
  }).catch((error) => console.log(error));
};

module.exports = {
  api: functions.https.onRequest(getAllUsers),
};