peerlibrary/meteor-peerdb

No references with collection: Meteor.users

Closed this issue · 7 comments

I have three documents, and the Person class hooks into Meteor.users

class @Person extends Document
  @Meta
    name: 'Person'
    collection: Meteor.users

class @Collection extends Document
  @Meta
    name: 'Collection'
    fields:  =>
      projects: [@ReferenceField Project, ['title']]
      creator: @ReferenceField Person, ['username']
      fields

class @Project extends Document
  @Meta
    name: 'Project'
    fields: =>
      collections: [@ReferenceField Collection, ['title']]
      creator: @ReferenceField Person, ['username']

When creating a user, Person is correctly taking him. Also, when I add Project or Collection items and reference them like

Project.documents.insert({title: 'My Project', collections: [{_id:  'Ct2Tn5Y5jQJRNedtc'}]})

it all works well and I can call Project.documents.findOne().collections[0].title

However, when I reference creator in either Project or Collection, only the _id field that I referenced appears.

Collection.documents.insert({title: 'Test', creator: {_id: 'ppqBNxxozyCfQATF6'}})

Also, Document.defineAll() is called after all definitions.

Any ideas why I don't get Collection.documents.findOne().username?

Why you are returning fields in fields callback in Collection document?

It should be:

class @Collection extends Document
  @Meta
    name: 'Collection'
    fields: =>
      projects: [@ReferenceField Project, ['title']]
      creator: @ReferenceField Person, ['username']

And you probably want Collection.documents.findOne().creator.username and not Collection.documents.findOne().username.

thanks, problem solved

@mitar you reference fields in fields in the documentation here https://github.com/peerlibrary/meteor-peerdb#reverse-references.

Yes, but there you are extending an existing document.

Ah, that makes sense. My bad.