【发布时间】:2017-01-30 06:18:17
【问题描述】:
我在.then 和.then 之外记录src 变量的值,我观察到src 具有不同的值,我想知道为什么:
getUserPicSrc(){
var src;
var db=new window.PouchDB('http://127.0.0.1:5984/passport-test');
db.getAttachment(this.props.store.user._id,'pic.jpg').then(blob=>{
src=window.blobUtil.createObjectURL(blob);
console.log('src inside then: '+src);// src inside then: blob:http://127.0.0.1:10002/ed1cf453-3902-4064-8966-e74c4a1ee6b4
}).catch(error=>{
console.log(error);
})
console.log('src outside then: '+src);// src outside then: undefined
return src;
}
更新
我最终这样做了,效果很好:
setUserPicSrc(){
var db=new window.PouchDB('http://127.0.0.1:5984/passport-test');
db.getAttachment(this.props.store.user._id,'pic.jpg').then(blob=>{
var url=window.blobUtil.createObjectURL(blob);
console.log('url: '+url);
document.getElementById('userPic').src=url;
}).catch(error=>{
console.log(error);
})
}
【问题讨论】:
-
另见 cmets 和这个问题的答案:stackoverflow.com/q/41788626/1078886
标签: javascript