我从Firebase Storage的UploadTask遇到问题。 我正在尝试上传图像,然后将图像URL保存在Firestore Cloud中。为此,我有以下代码:
newuploadImage() {
this.image = 'movie-' + new Date().getTime() + '.jpg';
let storageRef: any,
parseUpload: any;
storageRef = firebase.storage().ref('imgs/' + this.image);
parseUpload = storageRef.putString(this.cameraImage, 'data_url')
parseUpload.on('state_changed',
function (snapshot) {
}, function (error) {
}, function () {
// Upload completed successfully, now we can get the download URL
parseUpload.snapshot.ref.getDownloadURL().then(function (downloadURL) {
const idPubli = this.firestore.createId();
const idReto = this.firestore.createId();
let IDUser = firebase.auth().currentUser.uid;
let img = downloadURL
const idPuntPubli = this.firestore.createId();
let participacionCreadora = true;
let punt = 0;
this.firestore.doc(`public/${idPubli}`).set({
idPubli,
idReto,
IDUser,
img,
idPuntPubli,
participacionCreadora,
punt,
})
});
})
}
该图像可以很好地上传,但是当它尝试执行 this.firestore 时,控制台日志显示了以下错误:
错误TypeError:无法读取未定义的属性“ firestore”
我不知道如何克服这个错误。
编辑
这是导入和构造函数,以防出现问题。
import { AngularFirestore} from 'angularfire2/firestore';
constructor(public camera: Camera, public firestore: AngularFirestore) {
}
答案 0 :(得分:1)
您很可能遇到了示波器问题,可以通过替换此功能来解决该问题:
.then(function (downloadURL)
具有箭头功能(保留了词法范围)。
.then((downloadURL) =>
答案 1 :(得分:0)
共享我的代码,也许会对您有所帮助。
this.imageFile.name.substr(this.imageFile.name.lastIndexOf('.'));
const fileRef = this.storage.ref(this.filePath);
this.task = this.storage.upload(this.filePath, this.imageFile);
this.uploadPercent = this.task.percentageChanges();
this.task
.snapshotChanges()
.pipe(
finalize(() => {
this.downloadURL = fileRef.getDownloadURL();
this.downloadURL.subscribe(url => {
this.imageUrl = url;
});
})
)
.subscribe();
return this.uploadPercent;
}