@Injectable()
export class OrdersService {
private currentUserId?: string;
setUser(id: string): void { this.currentUserId = id; }
async myOrders(): Promise<Order[]> {
await this.audit.log("list");
return this.repo.findByUser(this.currentUserId!);
}
}
The service is a singleton, so concurrent requests overwrite the same
currentUserIdThe default scope is a singleton: one OrdersService instance serves every request. Request A sets currentUserId, then waits at await. Meanwhile request B sets it to B's id. When A continues, it reads B's id. Pass request data as method arguments instead. Use AsyncLocalStorage or a request-scoped provider only if you must.
"private fields are shared between instances" is wrong. Each instance has its own fields. The problem is that there is only one instance.
Read more