gpt4 book ai didi

javascript - 在同一组件内调用函数表示 this.checkUpdate 不是函数

转载 作者:行者123 更新时间:2023-12-01 03:53:36 25 4
gpt4 key购买 nike

我有以下上传功能,可将文件上传到 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

最佳答案

改变

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);
}
});

关于javascript - 在同一组件内调用函数表示 this.checkUpdate 不是函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42967903/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com