【问题标题】:Firebase Cloud Function to copy user email to database collectionFirebase 云功能将用户电子邮件复制到数据库集合
【发布时间】:2019-07-23 15:12:41
【问题描述】:

我想使用 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)

有什么想法吗?

【问题讨论】:

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


    【解决方案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),
    };
    

    【讨论】:

    • 嗨@ogot 找到解决方案做得很好!你能accept your own answer吗?它将使其更加明显,并在您找到解决方案时帮助遇到相同问题的人。谢谢!
    猜你喜欢
    • 1970-01-01
    • 2021-10-05
    • 2020-04-23
    • 2018-11-15
    • 1970-01-01
    • 2020-11-13
    • 2017-12-03
    • 2020-11-22
    • 2021-06-18
    相关资源
    最近更新 更多