我有以下上传功能,将文件上传到s3
_.each(files, file => {
let uuid = this.appMsgService.getGUID();
let bucket = new AWS.S3({ params: { Bucket: 'myBucket' } });
let params = { Key: `\source/${uuid}.pdf`, Body: file };
this.fileNames[file.name] = `${uuid}.pdf`;
this.fileName = `${uuid}`;
bucket.upload(params, function (error: any, res: any) {
uploadCount += 1;
console.log('error', error);
console.log('response', res);
if (uploadCount === files.length) {
this.interval = setInterval(() => {
console.log(JSON.stringify(this.fileNames));
this.checkUpdate(this.fileNames);
}, 3000);
}
});
});
我在同一个组件中有另一个函数,使用计时器调用并检查更新
checkUpdate(filenames: any) {
}
我已在上传的回调中添加了方法调用,但是它显示错误_this.checkUpdate is not a function
答案 0 :(得分:2)
更改
bucket.upload(params, function (error: any, res: any) {
到
bucket.upload(params, (error: any, res: any)=> {
您的this
未提及您的组件。
老js方式:
var self = this; //<-- use self to refer to the component in the lexical scope
bucket.upload(params, function (error: any, res: any) {
uploadCount += 1;
console.log('error', error);
console.log('response', res);
if (uploadCount === files.length) {
self.interval = setInterval(() => {
console.log(JSON.stringify(self.fileNames));
self.checkUpdate(self.fileNames);
}, 3000);
}
});