Firebase 'bytesTransferred':属性 'bytesTransferred' 在类型 'Object' 上不存在
Firebase 'bytesTransferred': Property 'bytesTransferred' does not exist on type 'Object'
我在用什么
- Angular
- Firebase
我有什么
- 允许我上传到 firebase 存储和实时数据库的上传服务
- 这几个星期以来一直运行良好
问题
今天早上没用。
我没有更改与此文件或其组件有关的任何内容
我唯一更改的是 firebase npm install 以尝试让其他一些废话起作用
这实际上完全停止了我的项目
我的服务现在显示构建错误:
[ts] Property 'bytesTransferred' does not exist on type 'Object'
[ts] Property 'totalBytes' does not exist on type 'Object'.
Argument of type '() => void' is not assignable to parameter of type 'Unsubscribe'.
Type 'void' is not assignable to type 'undefined'.
问题
- 有谁知道为什么突然显示这个错误消息?
我的上传服务
import { Injectable, OnDestroy } from '@angular/core';
import { Upload } from './upload';
import { AngularFireDatabase, FirebaseListObservable, FirebaseObjectObservable } from 'angularfire2/database';
import * as firebase from 'firebase';
import { AuthService } from '../../user/auth.service';
import { Subscription } from 'rxjs/Subscription';
// Routes
import { Router, ActivatedRoute } from '@angular/router';
@Injectable()
export class ProjectsAddService implements OnDestroy {
private _subscription: Subscription;
private basePath: string = '/site_photo';
uploads: FirebaseListObservable<Upload[]>;
constructor(
private db: AngularFireDatabase,
private authService: AuthService,
private router: Router
) { }
getUploads(query = {}) {
this.uploads = this.db.list(this.basePath, {
query: query
});
return this.uploads
}
deleteUpload(upload: Upload) {
this.deleteFileData(upload.$key)
.then(() => {
this.deleteFileStorage(upload.name)
})
.catch(error => console.log(error))
}
// Writes the file details to the realtime db
private deleteFileData(key: string) {
return this.db.list(`${this.basePath}/`).remove(key);
}
// Firebase files must have unique names in their respective storage dir
// So the name serves as a unique key
private deleteFileStorage(name: string) {
const storageRef = firebase.storage().ref();
storageRef.child(`${this.basePath}/${name}`).delete()
}
ngOnDestroy() {
if (this._subscription) {
this._subscription.unsubscribe();
}
}
submitForm(title: string, reference: string, date: string, auditorName: string, newCompanyName: string, upload: Upload) {
this._subscription = this.authService.user.subscribe(user => {
if (user) {
// The User ID of the current user
var userId = user.uid;
// Generate a new firebase key to be used for the Project, Project List, Project Members and Storage References
var newPostKey = firebase.database().ref().child('project_list').push().key;
// Create a reference to the firebase storage
const storageRef = firebase.storage().ref();
const uploadTask = storageRef.child('site_photo/' + user.uid + '/' + newPostKey + '/site_photo').put(upload.file);
uploadTask.on(firebase.storage.TaskEvent.STATE_CHANGED,
(snapshot) => {
// upload in progress
upload.progress = (snapshot.bytesTransferred / snapshot.totalBytes) * 100
},
(error) => {
// upload failed
console.log(error)
},
() => {
// upload success
upload.url = uploadTask.snapshot.downloadURL
upload.name = upload.file.name
// Data to be added to the 'Project' node
var albumDetails = {
album_title: title,
label_name: newCompanyName,
album_photo: upload.url
}
// Data to be added to the 'Project_list' node
var albumList = {
label_name: newCompanyName,
album_photo: upload.url
}
// Data to be added to the 'Project_members' node
var userList = {
[userId]: true
}
// Group the updates to the associated firebase nodes
var updates = {};
updates['/album/' + user.uid + '/' + newPostKey] = projectsDetails;
updates['/album_list/' + user.uid + '/' + newPostKey] = projectsList;
updates['/project_members/' + newPostKey] = userList;
// Once the image has been successfully uploaded, make an update to the realtime database
this.saveToRealtimeDatabase(updates);
this.router.navigate(['projects']);
}
);
}
});
}
// Perform an atomic Multi-Path update
private saveToRealtimeDatabase(updates) {
return firebase.database().ref().update(updates);
}
}
如有任何帮助,我们将不胜感激。完全被这个搞糊涂了。
更新
我注意到如果我注释掉以下代码,它会正确编译。我只是错误地读取了上传进度。
(snapshot) => {
// upload in progress
upload.progress = (snapshot.bytesTransferred / snapshot.totalBytes) * 100
},
(error) => {
// upload failed
console.log(error)
},
第二次更新
以下工作,但感觉有点奇怪。任何帮助将不胜感激。
var snapshotRef = snapshot as firebase.storage.UploadTaskSnapshot;
var bytesTransferred = (snapshotRef).bytesTransferred;
var totalBytes = (snapshotRef).totalBytes;
upload.progress = (bytesTransferred / totalBytes) * 100;
我对 typescript 或这个包的了解还不够,无法准确解释发生了什么,但至少我可以修复它。
显然新版本中的 complete 回调不再接受 any 作为 return 类型,而只接受 undefined。我认为这与 return 未定义的默认取消订阅有关。在您的代码中,您可以通过在回调中手动 returning undefined 来修复错误。
() => {
console.log('upload complete');
return undefined;
}
Typescript 显然无法在第一个回调中推断出快照的类型,您也需要手动指定它,您可以使用类型转换,也可以从回调中调用单独的函数。
console.log((snapshot as firebase.storage.UploadTaskSnapshot).bytesTransferred);
如果有人有更深入的解释,我将不胜感激。
用以下代码替换您的代码片段。
uploadTask.on(firebase.storage.TaskEvent.STATE_CHANGED,
(snapshot) => {
upload.progress = (uploadTask.snapshot.bytesTransferred / uploadTask.snapshot.totalBytes) * 100;
console.log(upload.progress);
},
我也遇到了同样的问题
我通过在构造函数之前声明 uploadtask 和所有其他变量然后简单地使用 this.uploadTask 来解决它
storageRef = firebase.storage().ref();
file : any;
uploadTask: any;
this.uploadTask = this.storageRef.child('images/${this.file.name}').put(this.file,metadata);
this.uploadTask.on(firebase.storage.TaskEvent.STATE_CHANGED, function(snapshot){...}
有同样的问题。通过设置快照类型修复。这是打字稿。
uploadTask.on('state_changed', (snapshot: firebase.storage.UploadTaskSnapshot) => {
upload.progress = (snapshot.bytesTransferred / snapshot.totalBytes) * 100
}
我在用什么
- Angular
- Firebase
我有什么
- 允许我上传到 firebase 存储和实时数据库的上传服务
- 这几个星期以来一直运行良好
问题
今天早上没用。
我没有更改与此文件或其组件有关的任何内容
我唯一更改的是 firebase npm install 以尝试让其他一些废话起作用
这实际上完全停止了我的项目
我的服务现在显示构建错误:
[ts] Property 'bytesTransferred' does not exist on type 'Object'
[ts] Property 'totalBytes' does not exist on type 'Object'.
Argument of type '() => void' is not assignable to parameter of type 'Unsubscribe'. Type 'void' is not assignable to type 'undefined'.
问题
- 有谁知道为什么突然显示这个错误消息?
我的上传服务
import { Injectable, OnDestroy } from '@angular/core';
import { Upload } from './upload';
import { AngularFireDatabase, FirebaseListObservable, FirebaseObjectObservable } from 'angularfire2/database';
import * as firebase from 'firebase';
import { AuthService } from '../../user/auth.service';
import { Subscription } from 'rxjs/Subscription';
// Routes
import { Router, ActivatedRoute } from '@angular/router';
@Injectable()
export class ProjectsAddService implements OnDestroy {
private _subscription: Subscription;
private basePath: string = '/site_photo';
uploads: FirebaseListObservable<Upload[]>;
constructor(
private db: AngularFireDatabase,
private authService: AuthService,
private router: Router
) { }
getUploads(query = {}) {
this.uploads = this.db.list(this.basePath, {
query: query
});
return this.uploads
}
deleteUpload(upload: Upload) {
this.deleteFileData(upload.$key)
.then(() => {
this.deleteFileStorage(upload.name)
})
.catch(error => console.log(error))
}
// Writes the file details to the realtime db
private deleteFileData(key: string) {
return this.db.list(`${this.basePath}/`).remove(key);
}
// Firebase files must have unique names in their respective storage dir
// So the name serves as a unique key
private deleteFileStorage(name: string) {
const storageRef = firebase.storage().ref();
storageRef.child(`${this.basePath}/${name}`).delete()
}
ngOnDestroy() {
if (this._subscription) {
this._subscription.unsubscribe();
}
}
submitForm(title: string, reference: string, date: string, auditorName: string, newCompanyName: string, upload: Upload) {
this._subscription = this.authService.user.subscribe(user => {
if (user) {
// The User ID of the current user
var userId = user.uid;
// Generate a new firebase key to be used for the Project, Project List, Project Members and Storage References
var newPostKey = firebase.database().ref().child('project_list').push().key;
// Create a reference to the firebase storage
const storageRef = firebase.storage().ref();
const uploadTask = storageRef.child('site_photo/' + user.uid + '/' + newPostKey + '/site_photo').put(upload.file);
uploadTask.on(firebase.storage.TaskEvent.STATE_CHANGED,
(snapshot) => {
// upload in progress
upload.progress = (snapshot.bytesTransferred / snapshot.totalBytes) * 100
},
(error) => {
// upload failed
console.log(error)
},
() => {
// upload success
upload.url = uploadTask.snapshot.downloadURL
upload.name = upload.file.name
// Data to be added to the 'Project' node
var albumDetails = {
album_title: title,
label_name: newCompanyName,
album_photo: upload.url
}
// Data to be added to the 'Project_list' node
var albumList = {
label_name: newCompanyName,
album_photo: upload.url
}
// Data to be added to the 'Project_members' node
var userList = {
[userId]: true
}
// Group the updates to the associated firebase nodes
var updates = {};
updates['/album/' + user.uid + '/' + newPostKey] = projectsDetails;
updates['/album_list/' + user.uid + '/' + newPostKey] = projectsList;
updates['/project_members/' + newPostKey] = userList;
// Once the image has been successfully uploaded, make an update to the realtime database
this.saveToRealtimeDatabase(updates);
this.router.navigate(['projects']);
}
);
}
});
}
// Perform an atomic Multi-Path update
private saveToRealtimeDatabase(updates) {
return firebase.database().ref().update(updates);
}
}
如有任何帮助,我们将不胜感激。完全被这个搞糊涂了。
更新
我注意到如果我注释掉以下代码,它会正确编译。我只是错误地读取了上传进度。
(snapshot) => {
// upload in progress
upload.progress = (snapshot.bytesTransferred / snapshot.totalBytes) * 100
},
(error) => {
// upload failed
console.log(error)
},
第二次更新 以下工作,但感觉有点奇怪。任何帮助将不胜感激。
var snapshotRef = snapshot as firebase.storage.UploadTaskSnapshot;
var bytesTransferred = (snapshotRef).bytesTransferred;
var totalBytes = (snapshotRef).totalBytes;
upload.progress = (bytesTransferred / totalBytes) * 100;
我对 typescript 或这个包的了解还不够,无法准确解释发生了什么,但至少我可以修复它。
显然新版本中的 complete 回调不再接受 any 作为 return 类型,而只接受 undefined。我认为这与 return 未定义的默认取消订阅有关。在您的代码中,您可以通过在回调中手动 returning undefined 来修复错误。
() => {
console.log('upload complete');
return undefined;
}
Typescript 显然无法在第一个回调中推断出快照的类型,您也需要手动指定它,您可以使用类型转换,也可以从回调中调用单独的函数。
console.log((snapshot as firebase.storage.UploadTaskSnapshot).bytesTransferred);
如果有人有更深入的解释,我将不胜感激。
用以下代码替换您的代码片段。
uploadTask.on(firebase.storage.TaskEvent.STATE_CHANGED,
(snapshot) => {
upload.progress = (uploadTask.snapshot.bytesTransferred / uploadTask.snapshot.totalBytes) * 100;
console.log(upload.progress);
},
我也遇到了同样的问题 我通过在构造函数之前声明 uploadtask 和所有其他变量然后简单地使用 this.uploadTask 来解决它
storageRef = firebase.storage().ref();
file : any;
uploadTask: any;
this.uploadTask = this.storageRef.child('images/${this.file.name}').put(this.file,metadata);
this.uploadTask.on(firebase.storage.TaskEvent.STATE_CHANGED, function(snapshot){...}
有同样的问题。通过设置快照类型修复。这是打字稿。
uploadTask.on('state_changed', (snapshot: firebase.storage.UploadTaskSnapshot) => {
upload.progress = (snapshot.bytesTransferred / snapshot.totalBytes) * 100
}