提问人:Mr.P 提问时间:11/14/2023 更新时间:11/14/2023 访问量:24
Node JS 在 promise [duplicate] 中使用私有函数
Node JS using private function inside promise [duplicate]
问:
我有以下代码:
export class TestClass {
#dbConnection;
connectToDatabase = async () => {
this.#dbConnection = mysql.createConnection({
host: 'xx.xx.xx.xx',
user: 'user',
password: 'password',
database: 'gtfs'
});
return await this.#dbConnection.connect();
}
doSomething = async () => {
return new Promise((resolve, reject) => {
this.#dbConnection.query(query, function (error, result) {
if (err) reject(err);
let privateThing = this.#doSomethingPrivate();
resolve(privateThing);
})
});
}
#doSomethingPrivate = () => {
return 'all good';
}
}
let test = new TestClass()
test.connectToDatabase()
.then(doSomething)
.then((data) => {
console.log(data);
});
但是在里面,我实际上不能打电话,因为它是私人的。我该怎么做?doSomething
#doSomethingPrivate
答: 暂无答案
评论
doSomething
是一种方法。您需要一个实例。它要么是,要么是.then(test.doSomething.bind(test))
.then(() => test.doSomething())
this