簡體   English   中英

如何在一個生成函數中使用一個高階函數的變量?

[英]How can i use a variable of one Higher Order Functions in one generation function?

需要使用email = user.emailnewcomment['comments/'+id] = {id,comment,email,date} ,但我不能使用email = yield user.emailyield auth.onAuthStateChanged(user => {email = user.email})和電子郵件歸因與空newcomment 我該怎么做?

export function* createComments(action){  
let email = null
try{
    auth.onAuthStateChanged(user => {
       email =  user.email
    })
    const id = yield database.ref().child("comments").push().key
    let date = new Date()
    date = yield date.getDate()+"/"+(date.getMonth()+1)+"/"+date.getFullYear()
    const newcomment = {}
    const comment = action.comment
    newcomment['comments/'+id] = {
        id,
        comment,
        email,
        date
    }
    database.ref().update(newcomment)
    yield put(ActionCreator.createCommentsSuccess(newcomment))
}catch({message}){
    yield put(ActionCreator.createCommentsFailure(message))
}

}

只要您只對第一次使用用戶調用onAuthStateChanged回調感興趣,您只需將其轉換為Promise:

const user = yield new Promise(resolve => {
  const unsub = auth.onAuthStateChanged(user => {
    if (user) {
      resolve(user);
      unsub();
    }
  });
});

暫無
暫無

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

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