我的云功能按預期運行,直到我嘗試將某些內容回傳到前端。對于最終的日志點 D 和 G,我仍然只收到{data:null},但對我的資料庫等的修改運行得非常好。
我想我可能錯誤地嘗試回傳該函式。
感謝您的寶貴時間,感謝所有幫助。
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);
});
}
});
uj5u.com熱心網友回復:
您需要正確回傳 Promises 鏈,如下所示(請參閱returns 以及then和catch塊的鏈接方式)。這是呼叫回傳 Promises 的背靠背異步方法時的關鍵,您可以在 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
}
});
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/516551.html
標籤:Google Cloud Collective javascript节点.js火力基地谷歌云功能
