函数 getAllTelephones 的奇怪行为
The function getAllTelephones strange behavior
Objective
我有三个表,分别是 clients
、contacts
和 telephones
。我有一条通往 return 所有电话的路线取决于 client.id
或 contact.id
如果我通过一个 client.id
我希望 return 我所有的电话都来自这个client
,同时 contacts
。但是这不起作用。我通过 client.id
return 给我所有电话,但如果 contact.id
return 给我一个空数组。如果我更改 if 位置,return 给我 contacts
电话,但 client
电话空了。
序列
telephones.routes.ts
telephonesRouter.get('/:id', telephonesController.index);
TelephonesController.ts
public async index(request: Request, response: Response): Promise<Response> {
const { id } = request.params;
try {
const telephoneService = container.resolve(GetTelephoneService);
const telephones = await telephoneService.execute({ id });
return response.status(200).json({ telephones });
} catch (err) {
return response.status(400).json({ error: err.message });
}
}
GetTelephoneService.ts
import { injectable, inject } from 'tsyringe';
import AppError from '@shared/errors/AppError';
/* Interface */
import IClientsRepository from '@modules/clients/repositories/IClientsRepository';
import IContactsRepository from '@modules/contacts/repositories/IContactsRepository';
import ITelephonesRepository from '../repositories/ITelephonesRepository';
import Telephone from '../infra/typeorm/entities/Telephone';
interface IRequest {
id: string;
}
@injectable()
class GetTelephoneService {
constructor(
@inject('TelephonesRepository')
private telephonesRepository: ITelephonesRepository,
@inject('ClientsRepository')
private clientsRepository: IClientsRepository,
@inject('ContactsRepository')
private contactsRepository: IContactsRepository,
) {}
public async execute({ id }: IRequest): Promise<Telephone[] | undefined> {
const isContact = await this.contactsRepository.findContact(id);
if (isContact !== undefined) {
const telephones = await this.telephonesRepository.getAllTelephones(id);
return telephones;
}
const isClient = await this.clientsRepository.findClient(id);
if (isClient !== undefined) {
const telephones = await this.telephonesRepository.getAllTelephones(id);
return telephones;
}
if (!isContact && !isClient) {
throw new AppError("We can't found the id", 400);
}
return undefined;
}
}
export default GetTelephoneService;
TelephonesRepository.ts
public async getAllTelephones(
owner_id: string,
): Promise<Telephone[] | undefined> {
const getTelephoneClient = await this.ormRepository.find({
where: { client_id: owner_id },
});
console.log('getTelephoneClient', getTelephoneClient);
const getTelephoneContact = await this.ormRepository.find({
where: { contact_id: owner_id, client_id: null },
});
console.log('getTelephoneContact', getTelephoneContact);
if (getTelephoneClient !== undefined) {
return getTelephoneClient;
}
if (getTelephoneContact !== undefined) {
return getTelephoneContact;
}
return undefined;
}
在TelephonesRepository.ts
if我先通过getTelephoneClient
if。将 returns 客户拥有的电话。但是,如果我通过 contact.id
,getTelephoneContact
就会变空。如果我反转,请反转行为。
我所做的解决方案是在 if's
我看到它们永远不会是 undefined
它们将包含 data
或将是一个空数组,因为 return 永远是 Telephone[] | undefined
。所以我验证了 array
长度是否不同 0.
Objective
我有三个表,分别是 clients
、contacts
和 telephones
。我有一条通往 return 所有电话的路线取决于 client.id
或 contact.id
如果我通过一个 client.id
我希望 return 我所有的电话都来自这个client
,同时 contacts
。但是这不起作用。我通过 client.id
return 给我所有电话,但如果 contact.id
return 给我一个空数组。如果我更改 if 位置,return 给我 contacts
电话,但 client
电话空了。
序列
telephones.routes.ts
telephonesRouter.get('/:id', telephonesController.index);
TelephonesController.ts
public async index(request: Request, response: Response): Promise<Response> {
const { id } = request.params;
try {
const telephoneService = container.resolve(GetTelephoneService);
const telephones = await telephoneService.execute({ id });
return response.status(200).json({ telephones });
} catch (err) {
return response.status(400).json({ error: err.message });
}
}
GetTelephoneService.ts
import { injectable, inject } from 'tsyringe';
import AppError from '@shared/errors/AppError';
/* Interface */
import IClientsRepository from '@modules/clients/repositories/IClientsRepository';
import IContactsRepository from '@modules/contacts/repositories/IContactsRepository';
import ITelephonesRepository from '../repositories/ITelephonesRepository';
import Telephone from '../infra/typeorm/entities/Telephone';
interface IRequest {
id: string;
}
@injectable()
class GetTelephoneService {
constructor(
@inject('TelephonesRepository')
private telephonesRepository: ITelephonesRepository,
@inject('ClientsRepository')
private clientsRepository: IClientsRepository,
@inject('ContactsRepository')
private contactsRepository: IContactsRepository,
) {}
public async execute({ id }: IRequest): Promise<Telephone[] | undefined> {
const isContact = await this.contactsRepository.findContact(id);
if (isContact !== undefined) {
const telephones = await this.telephonesRepository.getAllTelephones(id);
return telephones;
}
const isClient = await this.clientsRepository.findClient(id);
if (isClient !== undefined) {
const telephones = await this.telephonesRepository.getAllTelephones(id);
return telephones;
}
if (!isContact && !isClient) {
throw new AppError("We can't found the id", 400);
}
return undefined;
}
}
export default GetTelephoneService;
TelephonesRepository.ts
public async getAllTelephones(
owner_id: string,
): Promise<Telephone[] | undefined> {
const getTelephoneClient = await this.ormRepository.find({
where: { client_id: owner_id },
});
console.log('getTelephoneClient', getTelephoneClient);
const getTelephoneContact = await this.ormRepository.find({
where: { contact_id: owner_id, client_id: null },
});
console.log('getTelephoneContact', getTelephoneContact);
if (getTelephoneClient !== undefined) {
return getTelephoneClient;
}
if (getTelephoneContact !== undefined) {
return getTelephoneContact;
}
return undefined;
}
在TelephonesRepository.ts
if我先通过getTelephoneClient
if。将 returns 客户拥有的电话。但是,如果我通过 contact.id
,getTelephoneContact
就会变空。如果我反转,请反转行为。
我所做的解决方案是在 if's
我看到它们永远不会是 undefined
它们将包含 data
或将是一个空数组,因为 return 永远是 Telephone[] | undefined
。所以我验证了 array
长度是否不同 0.