我正在研究从typescript es6到javascript es6的节点应用程序。我使用inversify将类依赖注入到适当的类中。当我尝试使用this运算符访问成员变量时,我收到以下错误:" TypeError:无法读取属性' organizationMemberRepository'未定义"。我注意到在转到箭头功能并删除了"那="后,我开始收到这个错误。逻辑,我在docker容器中运行节点。这是已转换的javascript文件:
"use strict";
let OrganizationMemberService = class OrganizationMemberService {
constructor(organizationRepository, organizationMemberRepository, authorizationService) {
this.authenticateUser = (userName, password) => {
return new Promise(function (resolve, reject) {
console.log('about to check if user exists.....');
this.organizationMemberRepository.findMemberByUserName(userName)
.then(function (organizationMember) {
if (organizationMember) {
organizationMember.comparePassword(password)
.then(function (same) {
if (same) {
let returnedObj = JSON.parse(JSON.stringify(organizationMember));
resolve(returnedObj);
}
else {
reject(new genericerror_1.GenericError("You username/password combination is incorrect."));
}
})
.catch(function (err) {
reject(err);
});
}
else {
reject(new genericerror_1.GenericError("You username/password combination is incorrect."));
}
});
});
};
this.organizationRepository = organizationRepository;
this.organizationMemberRepository = organizationMemberRepository;
this.authorizationService = authorizationService;
}
};
OrganizationMemberService = __decorate([
inversify_1.injectable(),
__param(0, inversify_1.inject(types_1.TYPES.IOrganizationRepository)), __param(1, inversify_1.inject(types_1.TYPES.IOrganizationMemberRepository)), __param(2, inversify_1.inject(types_1.TYPES.IAuthorizationService)),
__metadata("design:paramtypes", [Object, Object, Object])
], OrganizationMemberService);
exports.OrganizationMemberService = OrganizationMemberService;
我认为es6 arrow运营商想要解决无法正确访问此运营商问题的问题?
答案 0 :(得分:1)
您需要将所有.then(function () {
语法更改为.then(()=> {
在this
正常工作之后,请参阅下面的更改
let OrganizationMemberService = class OrganizationMemberService {
constructor(organizationRepository, organizationMemberRepository, authorizationService) {
this.authenticateUser = (userName, password) => {
return new Promise( (resolve, reject) => {
console.log('about to check if user exists.....');
this.organizationMemberRepository.findMemberByUserName(userName)
.then( (organizationMember) => {
if (organizationMember) {
organizationMember.comparePassword(password)
.then( (same) => {
if (same) {
let returnedObj = JSON.parse(JSON.stringify(organizationMember));
resolve(returnedObj);
}
else {
reject(new genericerror_1.GenericError("You username/password combination is incorrect."));
}
})
.catch((err) => {
reject(err);
});
}
else {
reject(new genericerror_1.GenericError("You username/password combination is incorrect."));
}
});
});
};
this.organizationRepository = organizationRepository;
this.organizationMemberRepository = organizationMemberRepository;
this.authorizationService = authorizationService;
}
};