[英]Firebase Cloud Functions - How to call another function and return object
我已经从我的 Angular 组件创建了一个可调用函数。 我的角度组件调用createUser
函数并成功返回userRecord
值。
但是,我想做的是调用另一个名为createUserRecord
的云函数。 我不太熟悉承诺以及在这种特定情况下需要返回的内容。
下面是我的两个云函数。 我将如何在createUser
createUserRecord
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);
});
}
如果您想在创建用户时在 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 中链接函数吗?”?
如文档中所述,“云功能可以与特定触发器相关联”。 您可以通过创建相应的触发器来“链接”Cloud Functions,例如,在一个 CF 的 Firestore 中创建一个文档(例如可调用函数),这将触发另一个响应 Firestore 触发器的 CF。 话虽如此,在大多数情况下,通过链接 promises 而不是链接 Cloud Functions,您可能可以在一个独特的 Cloud Function 中满足很多需求。
最后,我不建议从云函数内部调用 HTTP 云函数,因为(恕我直言)HTTP 云函数更适合由外部消费者调用(我什至不知道这是否可行)。
Firebases 对此有意见会很有趣!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.