My cloud function runs as expected until I try to return something back to the front-end. For both final log points D and G, I still only receive {data:null}
, yet modifications to my databases etc, run perfectly fine.
I think I might be incorrectly trying to return the function.
Thanks for your time, all help is appreciated.
const functions = require("firebase-functions");
const admin = require("firebase-admin");
// // Create and Deploy Your First Cloud Functions
// // https://firebase.google.com/docs/functions/write-firebase-functions
admin.initializeApp();
exports.accountSetUp = functions.https.onCall((data, context)=>{
let validation = true;
if (data.company == "" || data.company == null || data.company == undefined) {
validation = false;
}
if (data.name == "" || data.name == null || data.name == undefined) {
validation = false;
}
const expression = /^[^@] @\w (\.\w ) \w$/;
if (!expression.test(data.email)) {
validation = false;
}
if (data.password.length < 8) {
validation = false;
}
if (data.type == "employer" && validation == true) {
const user = {
email: data.email,
emailVerified: false,
password: data.password,
displayName: data.name,
};
console.log("A");
admin.auth().createUser(user
).then((userReturn) => {
const companyDetails = {
admin: userReturn.uid,
company_name: data.company,
signed: {
sign: false,
name: "",
job: "",
date: "",
terms: "",
signature: "",
},
profile: {
size: "",
location: "",
logo: "",
li: "",
website: "",
company_description: "",
},
};
console.log("B");
admin.database().ref("/employers").push(companyDetails)
.then((companyReturn)=>{
const userDetails={
admin: true,
manager_num: userReturn.uid,
type: "employer",
email: data.email,
manager_name: data.name,
profile: {
job_title: "",
phone: "",
li: "",
location: ""},
company_num: companyReturn.key};
console.log("C");
admin.database().ref("/users/" userReturn.uid).set(userDetails)
.then(()=>{
console.log("d");
return ({result: "success"});
}).catch((error)=>{
console.log("E");
return (error);
});
}).catch((error)=>{
console.log("F");
return (error);
});
// add to user database
// add to company database
}).catch((error)=>{
console.log("G");
return (error);
});
}
});
CodePudding user response:
You need to correctly return the chain of Promises, as shown below (see the return
s and how the then
and catch
blocks are chained). This is key when calling back to back asynchronous methods which return Promises and you'll find some detailed explanations here on MDN.
exports.accountSetUp = functions.https.onCall((data, context) => {
let validation = true;
if (data.company == "" || data.company == null || data.company == undefined) {
validation = false;
}
if (data.name == "" || data.name == null || data.name == undefined) {
validation = false;
}
const expression = /^[^@] @\w (\.\w ) \w$/;
if (!expression.test(data.email)) {
validation = false;
}
if (data.password.length < 8) {
validation = false;
}
if (data.type == "employer" && validation == true) {
const user = {
email: data.email,
emailVerified: false,
password: data.password,
displayName: data.name,
};
console.log("A");
return admin.auth().createUser(user)
.then((userReturn) => {
const companyDetails = {
admin: userReturn.uid,
company_name: data.company,
signed: {
sign: false,
name: "",
job: "",
date: "",
terms: "",
signature: "",
},
profile: {
size: "",
location: "",
logo: "",
li: "",
website: "",
company_description: "",
},
};
console.log("B");
return admin.database().ref("/employers").push(companyDetails)
})
.then((companyReturn) => {
const userDetails = {
admin: true,
manager_num: userReturn.uid,
type: "employer",
email: data.email,
manager_name: data.name,
profile: {
job_title: "",
phone: "",
li: "",
location: ""
},
company_num: companyReturn.key
};
console.log("C");
return admin.database().ref("/users/" userReturn.uid).set(userDetails)
})
.then(() => {
console.log("d");
return ({ result: "success" });
}).catch((error) => {
console.log("E");
// See the doc: https://firebase.google.com/docs/functions/callable#handle_errors
});
} else {
// See the doc: https://firebase.google.com/docs/functions/callable#handle_errors
}
});