
[英]Read document in Firestore from firebase onCall cloud function
[英]Return a string from onCall firebase cloud function
我正在研究 firebase cloud function 并尝试使用 ZDE9B12E1515C25C7D8C0352F1413AB9A15Z auth 和 Cloud ZDE9B9ED78E2EF781E 函数注册用户。 我要做的是将数据发送到云端function,从云端function返回数据,然后在web app上使用。 我该怎么做? 这是两个代码:
Javascript 发送值,运行良好:
const sendData = firebase.functions().httpsCallable('registerUser');
sendData({
email: userEmail,
password: userPassword
});
在这里,我的云 function 正在正确注册用户。
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
}) });
我想从云 function 发送消息,然后在我的 javascript 代码上获取它。 我该怎么做?
如文档中所述,要“将数据发送回客户端,返回可以 JSON 编码的数据”并返回错误,您应该“抛出(或返回被拒绝的 Promise)一个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;
// ...
});
请参阅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;
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.