[英]how to handle promise from dto to entity with field promise?
I got a problem when mapping Dto to the entity where there is a column that has promises, when I mapped from dto to entity, in json there is __entity __... I want to get rid of this将 Dto 映射到有承诺的列的实体时遇到问题,当我从 dto 映射到实体时,在 json 中有 __entity __... 我想摆脱这个
I tried mapping 1 by 1, but this is irrelevant if the column is so many, I tried Object.assign successfully, but when in Return json there is still entity我尝试了 1 比 1 映射,但是如果列太多,这无关紧要,我尝试 Object.assign 成功,但是在 Return json 中仍然存在实体
this here are some code snippets这是一些代码片段
Class Entity Class 实体
experiences: Promise<Experience[]>;
portfolios: Promise<Portfolio[]>;
DTO DTO
experiences?: ExperiencePayload[];
portfolios?: PortfolioPayload[];
Function, Function,
public async getResumeByTalent(id: string): Promise<ResumePayload> {
const resume: Resume = await this.resumeRepository.findOne({ relations: ['experiences', 'addresses', 'languages', 'educations', 'portfolios'], where: {talentId: id}});
if (!resume) throw new BadRequestException(`Resume with Talent Id ${id} not found`);
const {languages, educations, experiences, addresses, portfolios} = resume;
const resumePayload: ResumePayload = new ResumePayload();
resumePayload.citizen = resume.citizen;
resumePayload.citizenCard = resume.citizenCard;
resumePayload.currentSalary = resume.currentSalary;
resumePayload.expectedSalary = resume.expectedSalary;
resumePayload.facebookProfile = resume.facebookProfile;
resumePayload.githubProfile = resume.githubProfile;
resumePayload.hobbies = resume.hobbies;
resumePayload.job = resume.job;
resumePayload.objective = resume.objective;
resumePayload.skill = resume.skill;
resumePayload.languages = await languages;
resumePayload.educations = await educations;
resumePayload.experiences = await experiences;
resumePayload.addresses = await addresses;
resumePayload.portfolios = await portfolios;
return resumePayload;
}
I tried the code snippet above, it worked, but too many mapped, and i try我尝试了上面的代码片段,它有效,但映射太多,我尝试
Object.assign(resumePayload, resume);
but return in json但返回 json
{
"id": "asasasa",
"talentId": "a6cb25db-21be-4303-b63e-c3dcbcd7b2a2",
"citizen": "99999292",
"citizenCard": "KTP",
"skill": null,
"expectedSalary": 100000,
"currentSalary": 100000,
"objective": "JASJASJAJS",
"hobbies": "JJJJJ",
"linkedinProfile": null,
"githubProfile": null,
"facebookProfile": null,
"job": null,
"createdAt": "2021-01-14T17:10:09.628Z",
"createdBy": "system",
"updatedAt": "2021-01-14T17:10:09.628Z",
"updatedBy": null,
"deletedAt": null,
"deletedBy": null,
"__experiences__": [],
"__addresses__": [],
"__languages__": [],
"__educations__": [],
"__portfolios__": []
}
i wan't remove ___education__
without to many mapped我不想删除___education__
没有很多映射
Regards问候
I'm sure libraries already exist for this, but I think a simple Typescript function may look as follows:我确定库已经存在,但我认为一个简单的 Typescript function 可能如下所示:
async function recursiveAwait(obj: any): Promise<any> {
if (Array.isArray(obj)) {
return (await Promise.all(obj.map(o => recursiveAwait(o))));
}
if (typeof obj === 'object') {
for (const key in obj) {
obj[key] = await recursiveAwait(obj[key]);
}
return obj;
}
return await obj;
}
It's not efficient but it wouldn't need any external libraries which might be useful if you don't want dependencies.它效率不高,但它不需要任何外部库,如果您不想要依赖项,这可能会很有用。
And note, it's really not efficient , it doesn't try to resolve all of the promises in parallel and it awaits variables that aren't promises which is not ideal.请注意,它确实效率不高,它不会尝试并行解决所有的承诺,它会等待不是承诺的变量,这不是理想的。
You also lose your typing, which could be solved by using templating:你也会丢失你的打字,这可以通过使用模板来解决:
sync function recursiveAwait<T, U>(obj: T): Promise<U>;
But then you have to define an object U
that is just the resolved version of T
.但是你必须定义一个 object U
,它只是T
的解析版本。
An example of using this function would look as follows:使用此 function 的示例如下所示:
async function run() {
let myPromises = {
arrayOfThem: [
new Promise(resolve => setTimeout(() => resolve(1), 1000)),
new Promise(resolve => setTimeout(() => resolve(2), 2000)),
],
oneOfThem: new Promise(resolve => setTimeout(() => resolve(3), 1500)),
objectOfThem: {
a: new Promise(resolve => setTimeout(() => resolve(4), 1000)),
b: new Promise(resolve => setTimeout(() => resolve(5), 2000)),
},
somethingElse: {
a: 'a',
b: ['b', 'c']
},
str: 'string'
}
console.log(myPromises);
myPromises = await recursiveAwait(myPromises);
console.log(myPromises);
}
run().then(() => console.log('done'));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.