简体   繁体   中英

Why can't I have async transformers in Mongoose?

Is there a reason I can't use an asynchronous transformer when defining a Mongoose schema? Here's an example:

const bookSchema = new Schema({
  title: {
    type: String,
    transform: async function(v) {
      return Promise.resolve(v)
    }
  },
});

Turns out this is impossible because transformers are called in toJSON and toObject which are synchronous functions. Instead, make use of middleware (not as clean, but it gets the job done).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM