【问题标题】:Reference.set failed: First argument contains undefinedReference.set 失败:第一个参数包含未定义
【发布时间】:2019-09-08 00:39:17
【问题描述】:

我创建了一个监听 onCreate 事件的 firebase 函数,但是 DocumentSnapshot.data() 返回为空。

功能码为:

exports.createClientAccount = functions.firestore
  .document('/userProfile/{userId}/clientList/{clientId}')
  .onCreate(async (snap, context) => {
    console.log('****snap.data(): ', snap.data()); //Showing Empty from the console.
    return admin
      .auth()
      .createUser({
        uid: context.params.clientId,
        email: snap.data().email,
        password: '123456789',
        displayName: snap.data().fullName,
      })
      .then(userRecord => {
        return admin
          .database()
          .ref(`/userProfile/${userRecord.uid}`)
          .set({
            fullName: userRecord.displayName, //ERROR here: Reference.set failed: First argument contains undefined
            email: userRecord.email,
            coachId: context.params.userId,
            admin: false,
            startingWeight: snap.data().startingWeight,
          });
      })
      .catch(error => {
        console.error('****Error creating new user',error);
      });
  });

该文档是在

下的 firebase 数据库上创建的
/userProfile/{userId}/clientList/{clientId}

clientId document created on the database

根据文档,onCreate 会在创建新文档时进行监听,并返回通过 DocumentSnapshot 接口创建的数据的快照。但是,我从控制台检查 snap.data() 是空的。如果在数据库上成功创建文档,我不明白为什么它是空的。

image showing error returned by the functions when creating the userProfile

从函数代码来看,return admin.auth.createUser({}) 正在将用户创建为匿名用户,因为 snap.data().email 未定义,但它应该创建一个非匿名用户。

【问题讨论】:

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


    【解决方案1】:

    首先,请尝试将document('/userProfile/{userId}/clientList/{clientId}')更改为document('userProfile/{userId}/clientList/{clientId}')

    path 不应以 / 开头。

    exports.createClientAccount = functions.firestore
      .document('userProfile/{userId}/clientList/{clientId}')
    

    【讨论】:

    • 嗨@zkohi 感谢您的帮助!最后的问题是,当我使用 add({}) 创建文档时,我没有在指令中包含字段。这是创建客户端文档的函数,现在该函数被正确触发 async clientCreate( fullName: string, email: string, startingWeight: number ): Promise { const newClientRef = await this.firestore .collection(userProfile/${this.userId}/clientList/ ) .add({ fullName, email, startingWeight: startingWeight * 1, });
    • 嗨@Jorge Willian Alvarado Jiménez 感谢您的评论!
    【解决方案2】:

    最后的问题是,当我使用 add({}) 创建文档时,我没有在指令中包含字段。这是创建客户端文档的函数,现在该函数被正确触发。

      async clientCreate(
        fullName: string,
        email: string,
        startingWeight: number
      ): Promise<any> {
        const newClientRef = await this.firestore
          .collection(`userProfile/${this.userId}/clientList/`)
          .add({
            fullName,
            email, 
            startingWeight: startingWeight * 1,
          });
    
        return newClientRef.update({
          id: newClientRef.id
        }); 
    }
    

    我在没有字段的情况下调用this.firestore.collection(...).add({}),所以当它发生时,云函数被触发并且DocumentSnapshot.data() 为空,因此返回Reference.set 错误。云函数createClientAccount是正确的。谢谢。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-14
      • 2018-08-08
      相关资源
      最近更新 更多