Firebase Cloud功能可将用户电子邮件复制到数据库集合

时间:2019-03-01 14:56:55

标签: firebase google-cloud-firestore google-cloud-functions

我想使用Cloud Functions将与每个用户关联的电子邮件复制到Firestore集合(“用户”)。集合中的每个文档都有用户的UID作为名称。我具有以下功能:

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),
};

对于无效数据,我收到以下错误消息:

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

有什么想法吗?

1 个答案:

答案 0 :(得分:1)

通过将数据预先转换为JSON来使其工作,这是工作功能:

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),
};