Firebase Cloud函数-如何调用另一个函数并返回对象

时间:2019-04-25 08:53:42

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

我已经从Angular组件中创建了一个可调用函数。我的角度分量调用了createUser函数,并成功返回了userRecord值。

但是,我想做的是调用另一个名为createUserRecord的云函数。在这种特殊情况下,我对Promise和需要返回的内容不太熟悉。

下面是我的两个云功能。 createUserRecord成功后,我该如何呼叫createUser

export const createUser = functions.https.onCall(async (data, context) => {
    console.log('data = ', data);
    return admin.auth().createUser({
        email: data.email,
        password: data.password,
    }).then(function (userRecord) {
        return userRecord
    })
        .catch(function (error) {
            return error;
            console.log('Error creating new user:', error);
        });
});




export const createUserRecord = functions.auth.user().onCreate((user, context) => {
    const userRef = db.doc(`users/${user.uid}`);
    return userRef.set({
        email: user.displayName,
        createdAt: context.timestamp,
        nickname: 'bubba',
    })
});

更新

这是我产生的版本,其中我将两个功能合并在一起。这确实产生了创建和帐户,然后写入Firestore的预期结果。但是,由于它没有向客户返回值,因此确实感觉有点“偏离”。

export const createUser = functions.https.onCall(async (data, context) => {
    console.log('data = ', data);
    return admin.auth().createUser({
        email: data.email,
        password: data.password,
    }).then(function (userRecord) {
        const userRef = db.doc(`users/${userRecord.uid}`);
        return userRef.set({
            email: data.email,
            name: data.name,
        })
    })
        .catch(function (error) {
            return error;
            console.log('Error creating new user:', error);
        });
});

角度可调用函数

sanitizedMessage控制台日志将返回未定义。

addUser() {

    const createUser = firebase.functions().httpsCallable('createUser');

    const uniquePassword = this.afs.createId();

    createUser({
      email: this.userForm.value.email,
      password: uniquePassword,
      name: this.userForm.value.name,
    }).then((result) => {
      // Read result of the Cloud Function.
      var sanitizedMessage = result.data.text;
      console.log('sanitizedMessage = ', sanitizedMessage);
    }).catch((error) => {
      var code = error.code;
      var message = error.message;
      var details = error.details;
      console.log('error = ', error);
    });
  }

1 个答案:

答案 0 :(得分:1)

如果您想在创建用户时在Firestore中创建记录,则可以在唯一的Cloud Function中很好地做到这一点。以下代码可以解决问题,并假设您要写入users Firestore集合。

const FieldValue = require('firebase-admin').firestore.FieldValue;

...
export const createUser = functions.https.onCall((data, context) => {
  console.log('data = ', data);
  return admin
    .auth()
    .createUser({
      email: data.email,
      password: data.password
    })
    .then(userRecord => {
      return admin.firestore().collection('users')
        .doc(userRecord.uid)
        .set({
          email: userRecord.displayName,
          createdAt: FieldValue.serverTimestamp(),
          nickname: 'bubba'
        });
    })
    .then(() => {
      return {
        result: 'Success'
      };
    })
    .catch(error => {
      //Look at the documentation for Callable Cloud Functions to adapt this part:
      //https://firebase.google.com/docs/functions/callable?authuser=0
    });
});

“是否有特定的原因不要求在CF中链接功能?” ?

documentation中所述,“云功能可以与特定触发器关联”。您可以通过创建相应的触发器来“链接” Cloud Functions,例如,在Firestore中的一个CF中创建文档(例如Callable Function),该文档将触发另一个响应Firestore触发器的CF。 话虽如此,在大多数情况下,您可以通过链接承诺而不是链接云功能来满足独特的Cloud Function中的许多需求

最后,我不建议从Cloud Function中调用HTTP Cloud Function,因为(IMHO)HTTP Cloud Functions的设计目的是由外部使用者调用(我什至不知道这样做是否可行)。

让Firebasers对此发表意见会很有趣!

相关问题