我正在研究firebase云功能,并尝试使用javascript和云功能在firebase auth上注册用户。 我要做的是将数据发送到云功能,从云功能返回数据,然后在Web应用程序上使用它。我该怎么做?这是两个代码:
JavaScript发送值,效果很好:
const sendData = firebase.functions().httpsCallable('registerUser');
sendData({
email: userEmail,
password: userPassword
});
这里是我的云功能,该功能可以正确注册用户。
exports.registerUser = functions.https.onCall((data, context) => {
const userEmail = data.email;
const userPassword = data.password;
admin.auth().createUser({
email: userEmail,
emailVerified: false,
password: userPassword,
displayName: "name",
photoURL: "...",
disabled: false
})
.then(() => {
//want to return messages if succeed or not
}) });
我想从云功能发送消息,然后在我的JavaScript代码上获取它。我该怎么办?
答案 0 :(得分:2)
如documentation中所述,要“将数据发送回客户端,返回可以JSON编码的数据”并返回错误,您应“抛出(或返回被拒绝的Promise)实例{ {1}}”。
functions.https.HttpsError
您可以使用不同的error codes,具体取决于错误的类型。参见doc。
然后,在客户端中,您需要执行以下操作:
exports.registerUser = functions.https.onCall((data, context) => {
const userEmail = data.email;
const userPassword = data.password;
return admin.auth().createUser({ // See the return here
email: userEmail,
emailVerified: false,
password: userPassword,
displayName: "name",
photoURL: "...",
disabled: false
})
.then(userRecord => {
//want to return messages if succeed or not
return {
result: 'success'
};
})
.catch(error => {
throw new functions.https.HttpsError('invalid-argument', 'message');
})
});
答案 1 :(得分:1)
请参见https://firebase.google.com/docs/functions/get-started#review_complete_sample_code
您做类似
exports.registerUser = functions.https.onCall((data, context) => {
const userEmail = data.email;
const userPassword = data.password;
const userRecord = admin.auth().createUser({
email: userEmail,
emailVerified: false,
password: userPassword,
displayName: "name",
photoURL: "...",
disabled: false
})
// userRecord is promise, not a value
return userRecord;
});