[英]How can I use higher order functions like Array.reduce() in an Indesign script?
[英]How can i use a variable of one Higher Order Functions in one generation function?
需要使用email = user.email
在newcomment['comments/'+id] = {id,comment,email,date}
,但我不能使用email = yield user.email
或yield 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.