使用以下代码时,即使在解析promise之前,Async操作“FILE_UPLOAD_SUCCESS”也会发出错误,因此操作有效内容将返回undefined。关于如何解决它的任何想法???
行动创作者
export const upload = (file) => {
return (dispatch)=>{
dispatch({type:"FILE_UPLOAD_START"})
return new Promise.resolve(()=>{
uploadDoc(file).then((hash)=>dispatch({type:"FILE_UPLOAD_SUCCESS",payload:hash}))
}).catch((err)=>{
dispatch({type:"FILE_UPLOAD_ERROR",payload:err})
})
}
}
uploadDoc
export const uploadDoc = (file) =>{
return new Promise((resolve,reject)=>{
var reader = new FileReader();
reader.readAsArrayBuffer(file[0]);
reader.onloadend = (evt) =>{
resolve(reader);
}
}).then((reader)=>{
return Promise.resolve(()=>{
const buffer = Buffer.from(reader.result);
ipfs.add(buffer)
.then((res)=>{
return res[0].hash;
})
})
})
}