簡體   English   中英

Firebase Cloud onCall function 總是返回 null

[英]Firebase Cloud onCall function return always null

我使用 firebase function 創建營地表格。 這個 function 工作正常,但我不明白為什么返回 null。 實際上在創建營地之前返回 null。我該怎么辦?

firebase雲function:

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
      }
    })
  }
})

如果您將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
    }
  }
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM