【发布时间】:2019-05-29 14:52:23
【问题描述】:
我有一个有效的 Firebase 云函数(它以管理员身份写入我的数据库,并且还检查具有电子邮件字符串 ID 的现有条目)。我的问题是它没有正确地将数据返回到客户端应用程序。
当我跟踪result 的值时,它是{data: null}
如果电子邮件存在,我也想抛出一个错误,但它抱怨未处理的承诺(请参阅下面代码中的 cmets)。
所以这里有两个问题。第一个是最紧迫的。
exports.saveData = functions.https.onCall( (data, context) => {
// check if the email exists
admin.database().ref('competition_entries/' + data.compId + '/' + data.emailStringId).once('value').then(function(snapshot) {
if (snapshot.val() == null){
console.log("Writing data to "+data.compId+" : "+data.emailStringId);
let current_datetime = new Date()
let formatted_time = current_datetime.getHours() + ":" + current_datetime.getMinutes() + ":" + current_datetime.getSeconds();
let formatted_date = current_datetime.getDate() + "/" + (current_datetime.getMonth() + 1) + "/" + current_datetime.getFullYear();
let timestamp_string = formatted_time+" "+formatted_date;
console.log(formatted_date)
admin.database().ref('competition_entries/' + data.compId + '/' + data.emailStringId).set({
name: data.name,
email: data.email,
phone: data.phone,
selectedItem: data.selectedItem,
submitted_date: timestamp_string
})
.then(() => {
console.log("success");
return {
"error": "none",
"message": "success"
};
}).catch(function(error) {
throw error;
});
}else{
// email address exists as a data entry so tell the user that they have already submitted
let code = "already-exists";
let message = "Email address already exists";
let details = "A user with the same email address has already entered the competition.";
console.log(message+"/"+details);
/*
TODO: throwing an HttpsError directly here was causing an unresolved promise error. This would be the better way to tell the client that the user has already submitted their data.
*/
// throw error;
//throw new functions.https.HttpsError(code, message, details);
// this resolves the promise error but is a hack
return {
error: new functions.https.HttpsError(code, message, details),
message: message
}
}
}).catch(function(error) {
throw error;
});
});
【问题讨论】:
标签: javascript firebase firebase-realtime-database google-cloud-functions