我有一个Angular 4项目,我将文件上传到firebase存储。我已将上传代码基于angular-firestarter。如果我运行此代码它工作正常。
我已将upload.service.ts包含在我的代码中,并且 ed 很好,但在离开它一段时间后,upload.service.ts将不再编译。我收到一个错误:'()=>类型的参数 void'不能分配给'Unsubscribe'类型的参数。 类型'void'不能分配给'undefined'类型。对于看起来像这样的函数:
pushUpload(upload: Upload, desktopKey: string) {
const storageRef = firebase.storage().ref();
const uploadTask = storageRef.child(`${this.basePath}/${upload.file.name}`).put(upload.file);
uploadTask.on(firebase.storage.TaskEvent.STATE_CHANGED,
(snapshot: any) => {
// 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;
this.saveDocumentData(upload, desktopKey);
}
);
}
似乎这个on on function的定义在我的项目中是错误的(如果我评论它,应用程序工作正常)。什么可以导致这样的问题以及如何解决它?
如果我比较两个项目的firebase.d.ts文件它们有点不同,尽管两个项目都有“firebase”:package.json中的“^ 4.1.3”
来自angular-firestarter的
interface UploadTask {
cancel ( ) : boolean ;
catch (onRejected : (a : Error ) => any ) : firebase.Promise < any > ;
on (event : firebase.storage.TaskEvent , nextOrObserver ? : null | Object , error ? : ( (a : Error ) => any ) | null , complete ? : ( ( ) => any ) | null ) : Function ;
pause ( ) : boolean ;
resume ( ) : boolean ;
snapshot : firebase.storage.UploadTaskSnapshot ;
then (onFulfilled ? : ( (a : firebase.storage.UploadTaskSnapshot ) => any ) | null , onRejected ? : ( (a : Error ) => any ) | null ) : firebase.Promise < any > ;
}
来自我的项目:
interface UploadTask {
cancel ( ) : boolean ;
catch (onRejected : (a : Error ) => any ) : firebase.Promise < any > ;
on (event : firebase.storage.TaskEvent , nextOrObserver ? : firebase.Observer < any , any > | null | ( (a : Object ) => any ) , error ? : ( (a : Error ) => any ) | null , complete ? : ( firebase.Unsubscribe ) | null ) : Function ;
pause ( ) : boolean ;
resume ( ) : boolean ;
snapshot : firebase.storage.UploadTaskSnapshot ;
then (onFulfilled ? : ( (a : firebase.storage.UploadTaskSnapshot ) => any ) | null , onRejected ? : ( (a : Error ) => any ) | null ) : firebase.Promise < any > ;
}
我删除了node_modules和package-lock.json并运行npm install,但它没有帮助。
我还尝试从包含node_modules的备份恢复我的项目然后它可以工作,但是当我删除node_modules和package-lock.json以及npm install时,我得到了错误。
所以我知道为什么我的项目中出现了错误的typedefinition文件,即使我安装了相同版本的firebase?
我的package.json文件;
{
"name": "docavea",
"version": "0.0.0",
"license": "MIT",
"scripts": {
"ng": "ng",
"start": "ng serve --o",
"build": "ng build",
"test": "ng test",
"lint": "ng lint",
"e2e": "ng e2e",
"populate-db": "./node_modules/.bin/ts-node ./populate-db.ts",
"populate-db2": "./node_modules/.bin/ts-node ./populate-db2.ts"
},
"private": true,
"dependencies": {
"@angular/animations": "^4.3.2",
"@angular/common": "^4.3.2",
"@angular/compiler": "^4.3.2",
"@angular/core": "^4.3.2",
"@angular/flex-layout": "^2.0.0-beta.8",
"@angular/forms": "^4.3.2",
"@angular/http": "^4.3.2",
"@angular/platform-browser": "^4.3.2",
"@angular/platform-browser-dynamic": "^4.3.2",
"@angular/platform-server": "^4.3.2",
"@angular/router": "^4.3.2",
"angularfire2": "^4.0.0-rc.1",
"core-js": "^2.4.1",
"firebase": "^4.1.3",
"global": "^4.3.2",
"lodash": "^4.17.4",
"ng2-pdf-viewer": "^1.1.0",
"ngx-bootstrap": "^1.7.1",
"rxjs": "^5.4.2",
"zone.js": "^0.8.12"
},
"devDependencies": {
"@angular/cli": "^1.2.6",
"@angular/compiler-cli": "^4.3.2",
"@types/jasmine": "2.5.38",
"@types/node": "~6.0.60",
"@types/lodash": "^4.14.67",
"codelyzer": "~2.0.0",
"jasmine-core": "~2.5.2",
"jasmine-spec-reporter": "~3.2.0",
"karma": "~1.4.1",
"karma-chrome-launcher": "~2.0.0",
"karma-cli": "~1.0.1",
"karma-coverage-istanbul-reporter": "^0.2.0",
"karma-jasmine": "~1.1.0",
"karma-jasmine-html-reporter": "^0.2.2",
"protractor": "~5.1.0",
"ts-node": "^3.1.0",
"tslint": "~4.5.0",
"typescript": "^2.4.2"
}
}
答案 0 :(得分:4)
问题来自已完成方法的处理程序:
() => {
// upload success
upload.url = uploadTask.snapshot.downloadURL;
upload.name = upload.file.name;
this.saveDocumentData(upload, desktopKey);
}
此函数的推断返回类型实际上是 void ,它与旧版本的firebase一起工作正常,因为期望的类型为()=&gt;任何但在新版本中,这已更改为 firebase.Unsubscribe ,其定义为:
type Unsubscribe = ( ) => undefined ;
因此,void的错误不能分配给undefined。
您可以通过多种方式解决此问题,例如,您可以从箭头函数返回undefined。