我正在学习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代码中得到它。我该怎么做呢?
发布于 2020-05-31 06:34:11
正如文档中所解释的那样,要“向客户端发送数据,返回可以被JSON编码的数据”,并返回错误,您应该“抛出(或返回拒绝的)一个functions.https.HttpsError
实例”。
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');
})
});
然后,在客户机中,您需要这样做:
sendData({
email: userEmail,
password: userPassword
}).then(function(response) {
// Read result of the Cloud Function.
var result = response.data.result;
// ...
}).catch(function(error) {
// Getting the Error details.
var code = error.code;
var message = error.message;
var details = error.details;
// ...
});
发布于 2020-05-31 06:34:04
请参阅代码
你做的事就像
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;
});
https://stackoverflow.com/questions/62117586
复制