【发布时间】:2019-05-05 19:17:33
【问题描述】:
我在使用 Firebase 存储的 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 时,控制台日志给了我这个错误:
ERROR TypeError: Cannot read property 'firestore' of undefined
我不知道如何克服这个错误。
编辑
这是导入和构造函数,以防出现问题。
import { AngularFirestore} from 'angularfire2/firestore';
constructor(public camera: Camera, public firestore: AngularFirestore) {
}
【问题讨论】:
-
你能分享你的构造函数吗?让我知道你在哪里初始化 firestore?
-
我已经用导入和构造函数编辑了这个问题。据我所知,它应该是正确的,因为我在其他文件上做了同样的事情并且它一直有效。
标签: typescript firebase ionic-framework google-cloud-firestore firebase-storage