无法在Typescript中的函数内部调用函数
Can't call a function inside a function in Typescript
基本上我有一个 Angular 网页,它通过我的 NodeJS 应用接收的 POST 将文件上传到服务器。
当我试图通过 subirArchivo() 接收文件路径并通过名为 InsertaPersonas() 的函数发送它时,我的问题就来了。尝试了好几种方法,总是导致函数被调用为undefined,甚至连一次函数都没有进入
这是我的代码:
subirArchivo(req: Request, res: Response) {
var form = new IncomingForm();
let readStream;
var self = this;
this.insertaPersonas('a'); // function undefined
form.parse(req);
form.on('file', (field: any, file: any) => {
// Do something with the file
// e.g. save it to the database
// you can access it using file.path
console.log('file', file.name); //this works
readStream = fs.createReadStream(file.path); // this works
// console.log(file.path); //this works
self.insertaPersonas(file.path); //function undefined
});
form.on('end', () => {
res.json();
});
}
这是我的全部class:https://pastebin.com/b8a2E3EZ
您的问题可能是 self
参考。由于箭头功能,self=this
也是不需要的。
谁在呼叫 subirArchivo
?
我猜测那个范围内的 self
不是你的 uploadController class。
这看起来像是一个典型的绑定问题;
class MyClass {
other () {
console.log('enter')
}
fn () {
this.other()
}
}
const instance = Class()
instance.fn() // works
Promise.resolve()
.then(instance.fn.bind(instance)) // works
Promise.resolve()
.then(instance.fn) // fails: this.other is undefined, not a function
您可以尝试从您调用 subirArchivo
函数的位置找到它,并确保它像 myInstance.subirArchivo()
一样被调用,或者首先绑定实例,无论是在您引用它的地方 myInstance.subirArchivo.bind(myInstance)
,或者在构造函数中:
class UploadController {
constructor () {
this.subirArchivo = this.subirArchivo.bind(this)
}
insertaPersonas (...) { ... }
subirArchivo () { ... this.insertaPersonas(...) ... }
}
有关更多信息,请参阅 Use of the JavaScript 'bind' method
基本上我有一个 Angular 网页,它通过我的 NodeJS 应用接收的 POST 将文件上传到服务器。
当我试图通过 subirArchivo() 接收文件路径并通过名为 InsertaPersonas() 的函数发送它时,我的问题就来了。尝试了好几种方法,总是导致函数被调用为undefined,甚至连一次函数都没有进入
这是我的代码:
subirArchivo(req: Request, res: Response) {
var form = new IncomingForm();
let readStream;
var self = this;
this.insertaPersonas('a'); // function undefined
form.parse(req);
form.on('file', (field: any, file: any) => {
// Do something with the file
// e.g. save it to the database
// you can access it using file.path
console.log('file', file.name); //this works
readStream = fs.createReadStream(file.path); // this works
// console.log(file.path); //this works
self.insertaPersonas(file.path); //function undefined
});
form.on('end', () => {
res.json();
});
}
这是我的全部class:https://pastebin.com/b8a2E3EZ
您的问题可能是 self
参考。由于箭头功能,self=this
也是不需要的。
谁在呼叫 subirArchivo
?
我猜测那个范围内的 self
不是你的 uploadController class。
这看起来像是一个典型的绑定问题;
class MyClass {
other () {
console.log('enter')
}
fn () {
this.other()
}
}
const instance = Class()
instance.fn() // works
Promise.resolve()
.then(instance.fn.bind(instance)) // works
Promise.resolve()
.then(instance.fn) // fails: this.other is undefined, not a function
您可以尝试从您调用 subirArchivo
函数的位置找到它,并确保它像 myInstance.subirArchivo()
一样被调用,或者首先绑定实例,无论是在您引用它的地方 myInstance.subirArchivo.bind(myInstance)
,或者在构造函数中:
class UploadController {
constructor () {
this.subirArchivo = this.subirArchivo.bind(this)
}
insertaPersonas (...) { ... }
subirArchivo () { ... this.insertaPersonas(...) ... }
}
有关更多信息,请参阅 Use of the JavaScript 'bind' method