I have a code that gets Requisition
data from an external system like so:
init() {
const requisitionData = this.loginMb().pipe(
map(response => response.data.token),
switchMap(loginData => this.getRequisitions(loginData)),
map(response => response.data),
);
requisitionData.subscribe(this.processData);
}
Here is processData()
processData(response: any) {
const requisitions = response.content.data;
for (const p of requisitions) {
const req = new Requisition(); // Requisition is the TypeORM entity
req.title = p.title;
req.reference = p.reference;
// other fields
this.repo.save(requisition);
}
Here's the constructor
@Injectable()
export class RequisitionSync {
constructor(
@InjectRepository(Requisition) private readonly repo: Repository<Requisition>,
private readonly httpService: HttpService,
) {}
The problem is inside processData()
, this.repo
becomes undefined
. If I do console.log(this.repo)
inside init()
, it will not be undefined
.
subscribe
has a different lexicalthis
than the class itself. What happens if you justconsole.log(this)
in theprocessData
method? – Jay McDonielSafeSubscriber { ...
, here's the whole thing: pastebin.com/s1zPFBfh – Edwinthis
than the class. – Jay McDonielprocessData
be anasync
function? – troy