nestjs/mongoose

@Prop() schemaType on Map not working.

Closed this issue · 7 comments

I'm submitting a bug for @prop() on Map type


[ ] Regression 
[x] Bug report
[ ] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead post your question on Stack Overflow.

Current behavior

I want to use Map on schemaType with the new decorator syntax but...

import { Schema, Prop } from '@nestjs/mongoose';

@Schema()
export class Category extends Document{
   @Prop({type: Map})
   products
}

const CategorySchema = SchemaFactory.createForClass(Category);

console.log(CategorySchema.obj)

Then we got this as output

 { products: { type: {} } }

And I can't use Map feature on mongoose Model like the link below
https://mongoosejs.com/docs/schematypes.html#maps

Expected behavior

It has the different structure when we create with mongoose.Schema()

import * as mongoose from 'mongoose';

const CategorySchema = new mongoose.Schema({
   products:Map
});

console.log(CategorySchema.obj)

Then we got

   { products: { type: <ref *1> [Function: Map] { Map: [Circular *1] } } }

When I use the old syntax Map schemaType work fine.

Sorry about my english I tried to explained the scenario.

Have you tried using the raw() function?

@Prop(raw({
  type: Map,
  of: String
}))

@kamilmysliwiec Unfortunately, I tried your solution but still doesn't work.

So what I actually mean is it return the difference data type from Mongoose

When I use mongoose.Schema it return as Map.

const CatSchema = new mongoose.Schema({
   family:Map
});


// Somewhere in code
async testMap(){
   await this.catModel.remove();
   const cat = new this.catModel();
   cat.family = new Map();
   cat.family.set('KEY', 'VALUE');
   await cat.save();
   const result = await this.cat.findOne();
   console.log(result); 

    // { 
    //    _id: 5ecd5f79d7882864d765ca47, 
    //    slots: Map(1) { 'KEY' => 'VALUE' },  <-- this store as MAP 
    //    __v: 0
    // }
}
    // you can get with Map API 
    result.family.get('KEY')

But with @Schema(), @prop(raw(...)) or @prop() it return as Object

@Schema()
class Cat extends Document{
   @Prop({
      type: Map
   })
  family
}
const CatSchema = SchemaFactory.createForClass(Cat);


// Somewhere in code
async testMap(){
   await this.catModel.remove();
   const cat = new this.catModel();
   cat.family = new Map();
   cat.family.set('KEY', 'VALUE');
   await cat.save();
   const result = await this.cat.findOne();
   console.log(result); 
    // { 
    //    _id: 5ecd5f79d7882864d765ca47, 
    //   family: { "KEY" : "VALUE" } <=== this store as OBJECT
    //    __v: 0
    // }

   
}
    // you can get value by blanket syntax 
    result.family['KEY']
    // But..
    result.family.get('KEY') // Error

I can't reproduce your issue. Can you share a reproduction repository?

@kamilmysliwiec Here the reproduce repo.
https://github.com/veekungx/nestjs-mongoose-reproduct

Just run the application
Everything print out to the console.log()

This should be fixed in 7.0.2

@kamilmysliwiec same problem

@Prop(
      raw(
          {
            type: Map,
          }
      )
  )
  favorite_movie_ids?: Map<string, Date> | null;

alway returns favorite_movie_ids as an Object instead of Map

@kamilmysliwiec I running into the same issue. Doesn't look like to be fixed yet