Javascript 无法在承诺内使用变量
我正在尝试将图片上载到firebase存储,没有问题 但是,当我试图用图像URL和从表单中获取的其他内容设置状态时,我的变量(部分和价格)在承诺中是空的。下面是我的代码:Javascript 无法在承诺内使用变量,javascript,reactjs,promise,firebase-storage,Javascript,Reactjs,Promise,Firebase Storage,我正在尝试将图片上载到firebase存储,没有问题 但是,当我试图用图像URL和从表单中获取的其他内容设置状态时,我的变量(部分和价格)在承诺中是空的。下面是我的代码: handleForm = e =>{ e.preventDefault(); const {section, price, image} = e.target const file = image.files[0] const pictureName = userInforma
handleForm = e =>{
e.preventDefault();
const {section, price, image} = e.target
const file = image.files[0]
const pictureName = userInformation.name.replace(/ /g, "_");
if(file.size <= 1000000){
const myRoute = '/img/userPictures/' + pictureName;
const storageRef = firebase.storage().ref(myRoute);
const task = storageRef.put(file);
task.on('state_changed', snapshot =>{
console.log('Uploaded');
}, error =>{
console.log(error.message)
}, () =>{
task.snapshot.ref.getDownloadURL().then(downloadURL =>{
this.setState({
information: this.state.data.concat([{
section: section.value,
price: price.value,
image:downloadURL}])
})
});
})
e.currentTarget.reset();
this.notifySuccess('Information uploaded');
}else{
this.notifyError('Image should be less than 1 MB')
}
}
handleForm=e=>{
e、 预防默认值();
const{section,price,image}=e.target
const file=image.files[0]
const pictureName=userInformation.name.replace(//g,“”);
if(file.size){
console.log('upload');
},错误=>{
console.log(错误消息)
}, () =>{
task.snapshot.ref.getDownloadURL()。然后(downloadURL=>{
这是我的国家({
信息:this.state.data.concat([{
section:section.value,
价格:价格,价值,
图像:下载URL}])
})
});
})
e、 currentTarget.reset();
这是notifySuccess(“信息上传”);
}否则{
此.notifyError('映像应小于1 MB')
}
}
哪里有错误?谢谢 这是因为您正在回调外部使用
e.currentTarget.reset()
。
试着把它放在你的回调成功的内部,它应该像预期的那样工作
(如下图所示)
handleForm=e=>{
e、 预防默认值()
const{section,price,image}=e.target
const file=image.files[0]
const pictureName=userInformation.name.replace(//g,'.'''
if(file.size){
console.log('已上载')
},
错误=>{
console.log(错误消息)
},
() => {
task.snapshot.ref.getDownloadURL()。然后(downloadURL=>{
这是我的国家({
信息:this.state.data.concat([
{
section:section.value,
价格:价格,价值,
图片:下载网址
}
])
})
})
e、 currentTarget.reset()
此.notifySuccess('已上载信息')
}
)
}否则{
此.notifyError('映像应小于1 MB')
}
}
您能告诉我您在这里遇到了什么样的错误吗?当我试图设置状态时,部分和价格为空,它只设置带有下载URL的图像Hi,谢谢您的回答,当我尝试时,由于某种原因,它无法清除表单。别介意Sathak,我没有把它放在您建议的位置,它现在可以工作了,谢谢你的支持!
handleForm = e => {
e.preventDefault()
const {section, price, image} = e.target
const file = image.files[0]
const pictureName = userInformation.name.replace(/ /g, '_')
if (file.size <= 1000000) {
const myRoute = '/img/userPictures/' + pictureName
const storageRef = firebase.storage().ref(myRoute)
const task = storageRef.put(file)
task.on(
'state_changed',
snapshot => {
console.log('Uploaded')
},
error => {
console.log(error.message)
},
() => {
task.snapshot.ref.getDownloadURL().then(downloadURL => {
this.setState({
information: this.state.data.concat([
{
section: section.value,
price: price.value,
image: downloadURL
}
])
})
})
e.currentTarget.reset()
this.notifySuccess('Information uploaded')
}
)
} else {
this.notifyError('Image should be less than 1 MB')
}
}