简体   繁体   English

在同一组件内调用一个函数说this.checkUpdate不是一个函数

[英]Calling a function inside the same component says this.checkUpdate is not a function

I have the following upload function which uploads the files to s3 我有以下上传功能,可将文件上传到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);
        }
      });

    });

i have another function inside the same component which is called using a timer and check for the updates 我在同一组件中有另一个函数,该函数使用计时器调用并检查更新

checkUpdate(filenames: any) {
}

I have added the method call inside the callback of the upload, but it gives an error saying _this.checkUpdate is not a function 我在上载的回调中添加了方法调用,但是它给出了一个错误消息,指出_this.checkUpdate is not a function

Change 更改

bucket.upload(params, function (error: any, res: any) {

to

bucket.upload(params, (error: any, res: any)=> {

Your this is not refering to your component. 您的this不是指您的组件。

Old js way: 旧的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);
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM