Coder Social home page Coder Social logo

Transform decorator in class-transformer does not wait for asynchronous function and returns a Promise instead of the transformed value about class-transformer HOT 3 CLOSED

bansiddha-indexnine avatar bansiddha-indexnine commented on June 5, 2024
Transform decorator in class-transformer does not wait for asynchronous function and returns a Promise instead of the transformed value

from class-transformer.

Comments (3)

2astm avatar 2astm commented on June 5, 2024

the same

from class-transformer.

diffy0712 avatar diffy0712 commented on June 5, 2024

The Transform decorator signature calls for a function

transformFn: (params: TransformFnParams) => any,
with return type any, which is a bit misleading (as it will work with a promise in your case at least in ts). I do not see anything that is should support for async calls. it will not await for it I. (if it would support async I think the plainToInstance, instanceToInstance and instanceToPlain functions should support async as well.

The plainToInstance function should be called with the concrete data and awaited before it.

I think you would need something like

const resolvedValue = await something.organization;
const organisation = resolvedValue && 'name' in resolvedValue ? resolvedValue.name : null;

const user = plainToInstance(User, {..., organization});

I hope this helps

from class-transformer.

diffy0712 avatar diffy0712 commented on June 5, 2024

Closing as answered.
Note that there is an ongoing issue on async support. #549

from class-transformer.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.