Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nested object #47

Open
kevinresol opened this issue Jun 14, 2016 · 10 comments
Open

Nested object #47

kevinresol opened this issue Jun 14, 2016 · 10 comments

Comments

@kevinresol
Copy link

I have a schema like this:

new Schema({
    waitingList: {
        __type__: {
            current: {
                __type__: [{
                    date: {
                        __type__: Date
                    },
                    user: {
                        __type__: ObjectId,
                        ref: "User"
                    },
                }]
            },
            history: {
                __type__: [{
                    date: {
                        __type__: Date
                    },
                    user: {
                        __type__: ObjectId,
                        ref: "User"
                    },
                }]
            }
        }
    },
}, {
    autoIndex: true,
    typeKey: "__type__"
});

So, in essence a sample document looks like this:

{
  waitingList: {
    current: [],
    history: [{date: someDate, user: myUser}],
  }
}

In that case I want to deep populate user, but if I specify the path as "waitingList.history.user". The plugin crashes with error TypeError: Cannot read property 'paths' of undefined at this position:

var schemaPath = schema.paths[currentPath]

For some funny reason I am restricted from declaring sub-schema for those embeded documents. It is possible to make it work in such case?

@kevinresol
Copy link
Author

This is what I get if I log schemaPath at line 261

Mixed {
  path: 'waitingList',
  instance: 'Mixed',
  validators: [],
  setters: [],
  getters: [],
  options: { __type__: { current: [Object], history: [Object] } },
  _index: null }

So, at line 285 the assignment schema = schemaPath.schema will cause schema become undefined, causing the aforementioned crash.

@buunguyen
Copy link
Owner

buunguyen commented Jun 14, 2016

What is __type__?

@kevinresol
Copy link
Author

It is the typeKey
http://mongoosejs.com/docs/guide.html#typeKey

@buunguyen
Copy link
Owner

buunguyen commented Jun 14, 2016

If you can add a failed test to the bugs section, it would help.

kevinresol added a commit to kevinresol/mongoose-deep-populate that referenced this issue Jun 15, 2016
@kevinresol
Copy link
Author

Here you are: kevinresol@49bd67c

@buunguyen
Copy link
Owner

Where can I find out more about this usage of type?

For example, what is the difference between

var ItemSchema = new Schema({
  waitingList: {
    type: {
      current: {
        type: [{
          date: {type: Date},
          user: {type: Number, ref: "User.bug47"},
        }]
      }
    }
  }
})

and

var ItemSchema = new Schema({
  waitingList: {
    current: [{
      date: {type: Date},
      user: {type: Number, ref: "User.bug47"},
    }]
  }
})

@kevinresol
Copy link
Author

kevinresol commented Jun 15, 2016

I am not sure if there are any extra information out there other than the link I posted and the source code.
I think mongoose will force treating the the field as a type declaration if the field name matches the typeKey option (which defaults to "type")

AFAIK, there should be no different between the two examples, functionally.

@Yuliang-Lee
Copy link

Is this has solution?My code has same problems if key was named "type".

@kevinresol
Copy link
Author

@Yuliang-Lee your problem is probably unrelated to deep-populate
You may need to specify typeKey in the schema options.

@Yuliang-Lee
Copy link

@kevinresol I try to modify typeKey but deep-populate occurs error

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants