【问题标题】:How can i use a variable of one Higher Order Functions in one generation function?如何在一个生成函数中使用一个高阶函数的变量?
【发布时间】:2018-12-28 09:07:35
【问题描述】:

需要在newcomment['comments/'+id] = {id,comment,email,date} 中使用email = user.email,但我不能使用email = yield user.emailyield auth.onAuthStateChanged(user => {email = user.email}),并且电子邮件在newcomment 中被归为null。我该怎么做?

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

}

【问题讨论】:

    标签: javascript reactjs firebase-realtime-database redux redux-saga


    【解决方案1】:

    只要您只对用户第一次调用 onAuthStateChanged 回调感兴趣,您只需将其转换为 Promise:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-11-14
      • 1970-01-01
      • 2019-01-23
      • 1970-01-01
      • 2013-04-22
      • 2012-09-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多