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

Calling meteorMethod fails if document doesn't contain all fields #649

Closed
arggh opened this issue Oct 21, 2017 · 3 comments
Closed

Calling meteorMethod fails if document doesn't contain all fields #649

arggh opened this issue Oct 21, 2017 · 3 comments

Comments

@arggh
Copy link
Contributor

arggh commented Oct 21, 2017

I noticed a strange issue after updating to newest Astronomy.

One part of my app that I haven't touched in ages wasn't working anymore.

It's a simple scenario, where I fetch my data with only some of the fields included.

// used in the template
docs() { 
   return Doc.find({}, {
     fields: {
        title: 1,
        color: 1,
        order: 1,
        removed: 1
     }
   });
}

Then at some point I would then call a meteorMethod on one of these documents, provided via Astronomy behavior (one I forked from your sorting-behavior).

It used to work with v2.3.x, but after updating to all I get is

`Exception while simulating the effect of invoking '/Astronomy/execute' TypeError: Cannot read property 'order' of undefined
    at Class.methods.moveTo

I pin-pointed the bug to have appeared in v2.4.7. The same issue is also present on the newest release.

If I remove the fields - attribute from the method that gets my documents, it works with v2.4.7 and above.

@arggh
Copy link
Contributor Author

arggh commented Oct 21, 2017

I have a strong hunch It's lurking somewhere here: f056af9

@arggh
Copy link
Contributor Author

arggh commented Oct 21, 2017

Ok, I figured out the cause in this case:

I had declared my "sort" -behavior like so:

behaviors: {
    sort: {
      orderFieldName: 'order',
      hasRootField: true,
      rootFieldName: 'userId', // <--- culprit
    }
  }

If I add userId to the fields attribute when fetching documents, it works again.

This changed in v2.4.7, since before that it used to work without specifying the userId (aka. rootFieldName), as long as it was actually present on the client, or so it seems.

Here is the forked version of sort-behavior I'm using: https://github.com/arggh/meteor-astronomy-sort-behavior/tree/v2

Should I close this issue or should it be given some thought?

@lukejagodzinski
Copy link
Member

You could actually extend definition by adding the beforeFind event and extend the fields option so that both the orderFieldName and rootFieldName are always included.

if (this.options.hasRootField) {
  definition.fields[this.options.rootFieldName] = {
    type: String,
    optional: true,
    default: null
  };

  definition.events.beforeFind = (e) => {
    const doc = e.currentTarget;
    const options = e.options;
    if (options.fields) {
      options.fields[this.options.rootFieldName] = 1;
    }
  };
}

So I'm closing this issue as it's not related with some bug in Astronomy.

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

2 participants