У меня есть облачная функция Firebase, которая работает (она пишет в мою базу данных как администратор, а также проверяет существующую запись с идентификатором строки электронной почты). Моя проблема в том, что он не возвращает данные обратно клиентскому приложению должным образом.
Когда я прослеживаю значение result
, это {data: null}
Я также хотел бы выдать ошибку, если электронное письмо существует, но оно жалуется на необработанное обещание (см. Комментарии в коде ниже).
Итак, здесь есть два вопроса. Первое из них является наиболее актуальным.
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;
});
});