我使用 firebase 函数来创建营地表单。此函数正在运行,但我不明白为什么返回 null。实际上在创建camp之前返回null。我该怎么办?
firebase 云功能:
exports.createCamp = functions.https.onCall((data, context) => {
if (!context.auth) {
return { message: 'Authentication Required!', code: 401 };
}
else{
admin.database().ref("/users/"+context.auth.uid).once("value",snap=>{
if(!snap.child("forms").exists()){
var newId = admin.database().ref().push().key
admin.database().ref("/forms/"+newId).update({
place : data.place,
who : context.auth.uid,
description:data.description,
startDate : data.startDate,
endDate : data.endDate,
pp : data.pp,
name : data.name,
formId:newId,
chat:{"1":{message:"Form Chat Açıldı!",name:"Sistem"}}
}).then(t=>{
admin.database().ref('/users/'+ context.auth.uid+'/forms/'+newId).update({
place : data.place,
description:data.description,
startDate : data.startDate,
endDate : data.endDate,
formId:newId
}).then(tt=>{
admin.database().ref("/users/"+context.auth.uid+"/participateCamp/"+newId).update({participate:true})
return { message: 'success', code: 400 };//not work
})
})
}
else{
return { message: 'fail', code: 401 };//not work
}
})
}
})
答案 0 :(得分:2)
如果您在异步代码中使用 then
,请确保始终return
异步调用:
exports.createCamp = functions.https.onCall((data, context) => {
if (!context.auth) {
return { message: 'Authentication Required!', code: 401 };
}
else{
return admin.database().ref("/users/"+context.auth.uid).once("value",snap=>{
if(!snap.child("forms").exists()){
var newId = admin.database().ref().push().key
admin.database().ref("/forms/"+newId).update({
place : data.place,
who : context.auth.uid,
description:data.description,
startDate : data.startDate,
endDate : data.endDate,
pp : data.pp,
name : data.name,
formId:newId,
chat:{"1":{message:"Form Chat Açıldı!",name:"Sistem"}}
}).then(t=>{
return admin.database().ref('/users/'+ context.auth.uid+'/forms/'+newId).update({
place : data.place,
description:data.description,
startDate : data.startDate,
endDate : data.endDate,
formId:newId
}).then(tt=>{
return admin.database().ref("/users/"+context.auth.uid+"/participateCamp/"+newId).update({participate:true}).then(() => {
return { message: "success", code: 400 }; //not work
};)
})
})
}
else{
return { message: 'fail', code: 401 };//not work
}
})
}
})
您可以在更新后的代码中看到,我们需要 return
后面带有 then
的异步调用。否则, then 将离开代码流程,而根本不会像您提到的那样调用。
另一种方法是使用 async/await
。这将是与 async/await
相同的代码:
exports.createCamp = functions.https.onCall(async (data, context) => {
if (!context.auth) {
return { message: "Authentication Required!", code: 401 };
} else {
const snap = await admin
.database()
.ref("/users/" + context.auth.uid)
.once("value");
if (!snap.child("forms").exists()) {
var newId = admin.database().ref().push().key;
const t = await admin
.database()
.ref("/forms/" + newId)
.update({
place: data.place,
who: context.auth.uid,
description: data.description,
startDate: data.startDate,
endDate: data.endDate,
pp: data.pp,
name: data.name,
formId: newId,
chat: { 1: { message: "Form Chat Açıldı!", name: "Sistem" } },
});
const tt = await admin
.database()
.ref("/users/" + context.auth.uid + "/forms/" + newId)
.update({
place: data.place,
description: data.description,
startDate: data.startDate,
endDate: data.endDate,
formId: newId,
});
await admin
.database()
.ref("/users/" + context.auth.uid + "/participateCamp/" + newId)
.update({ participate: true });
return { message: "success", code: 400 };
} else {
return { message: "fail", code: 401 }; //not work
}
}
});