1
變量值不同的I日誌內.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;
}
UPDATE
最後我做這個工作正常:
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);
})
}
另請參閱評論和這個問題的答案:http://stackoverflow.com/q/41788626/1078886 –