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

Broken in Mongoose 5.x.x #12

Closed
zaubara opened this issue Feb 15, 2018 · 16 comments
Closed

Broken in Mongoose 5.x.x #12

zaubara opened this issue Feb 15, 2018 · 16 comments

Comments

@zaubara
Copy link

zaubara commented Feb 15, 2018

Hey,
I just tried mongoose-keywords with querymen (from the yeoman rest template) and upgraded all the packages; keywords seems to break with Mongoose 5.0.x, I'm afraid.
Offending line is:
this[field].addToSet(transform(value))
since this[field] is undefined.
Thanks!

@awojtczyk
Copy link

Same here, downgraded to make it work again :)
@diegohaz is this repo being maintained?

@diegohaz
Copy link
Owner

@awojtczyk Yes, but I still didn't try Mongoose v5

I can take a look on this by Sunday.

Would you guys like to investigate a solution in the meanwhile?

@zaubara
Copy link
Author

zaubara commented Feb 15, 2018

Awesome!

I wasn't able to find a solution until yet; from what I can tell, it's different in v5 when the parsePath methods are called; seemingly every api call now, ie. "_this" is now undefined.
Maybe there's something on https://github.com/Automattic/mongoose/blob/master/migrating_to_5.md that helps.

@zaubara
Copy link
Author

zaubara commented Feb 16, 2018

Correction, this[field] is undefined, where before there's an object with methods like addToSet.

@diegohaz
Copy link
Owner

diegohaz commented Feb 20, 2018

After some hours of suffering, I still can't find a solution. It seems to be a bug in Mongoose, but I'm not sure. Docs aren't clear about it either.

Created an issue there: Automattic/mongoose#6155

@diegohaz
Copy link
Owner

It seems the bug was fixed on mongoose, and will be shipped in v5.0.10. Could you guys try it when it gets released?

I'm going to close the issue right now, but I can reopen if the issue remains after v5.0.10

@peteashworth
Copy link

peteashworth commented Mar 27, 2018

Getting the same error on the same line; using mongoose v5.0.11 and mongoose-keywords v0.3.2

Seems to work with findById but findOne or find fails.

@diegohaz diegohaz reopened this Mar 27, 2018
@vkarpov15
Copy link

@AshworthHub can you please provide some code samples? Considering that findById() is just a thin wrapper for findOne() it sounds very strange that findById() works but not findOne()

@peteashworth
Copy link

Note; Project was started off @diegohaz rest generator.

Account Model
accountSchema.plugin(mongooseKeywords, { paths: ['name', 'tags'] });

export const getShop = ({ params, query }, res, next) => {
  console.log('get shop');
  const accountId = get(params, 'accountId');
  const shopName = get(query, 'shop');
  if (accountId) {
    console.log('accountId', accountId);
    Account.findById(accountId)
      .then(notFound(res))
      .then(account => account.view())
      .then(success(res))
      .catch(next);
  } else if (shopName) {
    console.log('shopName', shopName);
    Account.findOne({ name: shopName })
      .then(notFound(res))
      .then(account => account.view())
      .then(success(res))
      .catch(next);
  }
};

/58929ff994f0e70007f9da33 works as it will use the accountId section.

?shop=testing gives the following error

TypeError: Cannot read property 'addToSet' of undefined
    at parsePath (/Users/peterashworth/Projects/salon-api/node_modules/mongoose-keywords/dist/index.js:60:24)
    at model.Query.<anonymous> (/Users/peterashworth/Projects/salon-api/node_modules/mongoose-keywords/dist/index.js:69:9)
    at SchemaString.SchemaType._applySetters (/Users/peterashworth/Projects/salon-api/node_modules/mongoose/lib/schematype.js:693:22)
    at SchemaString.SchemaType.applySetters (/Users/peterashworth/Projects/salon-api/node_modules/mongoose/lib/schematype.js:717:16)
    at SchemaString.SchemaType._castForQuery (/Users/peterashworth/Projects/salon-api/node_modules/mongoose/lib/schematype.js:1095:15)
    at SchemaString.castForQuery (/Users/peterashworth/Projects/salon-api/node_modules/mongoose/lib/schema/string.js:515:15)
    at SchemaString.SchemaType.castForQueryWrapper (/Users/peterashworth/Projects/salon-api/node_modules/mongoose/lib/schematype.js:1064:15)
    at cast (/Users/peterashworth/Projects/salon-api/node_modules/mongoose/lib/cast.js:300:32)
    at model.Query.Query.cast (/Users/peterashworth/Projects/salon-api/node_modules/mongoose/lib/query.js:3198:12)
    at model.Query.Query._castConditions (/Users/peterashworth/Projects/salon-api/node_modules/mongoose/lib/query.js:1278:10)
    at model.Query.Query._findOne (/Users/peterashworth/Projects/salon-api/node_modules/mongoose/lib/query.js:1494:8)
    at /Users/peterashworth/Projects/salon-api/node_modules/kareem/index.js:276:8
    at /Users/peterashworth/Projects/salon-api/node_modules/kareem/index.js:77:15
    at _combinedTickCallback (internal/process/next_tick.js:131:7)
    at process._tickDomainCallback (internal/process/next_tick.js:218:9)

@pthieu
Copy link

pthieu commented Apr 13, 2018

Adding my case in here as well, both .find() and .findOne() does not work.
if i remove the line userSchema.plugin(mongooseKeywords, { paths: [...]}) from my schema file, it works

@vkarpov15
Copy link

Thanks, I opened up a mongoose issue above ☝️ so we can track it in our milestones

@diegohaz
Copy link
Owner

Released v0.4.0 with @lineus's changes in #13. Hopefully, now it fully supports mongoose v5 (at least, all tests are passing).

Thank you so much for the time you guys invested on this.

@nickhingston
Copy link
Contributor

I'm not convinced this is fixed, it seems that Model.create causes the same problem with mongoose 5.9.7

seems this[field] is undefined on line 35 of src/index.js.
so:
this[field].addToSet(transform(value))
crashes

Looking at the call stack & in mongoose code:

 // By default, defaults get applied **before** setting initial values
    // Re: gh-6155
    $__applyDefaults(this, fields, skipId, exclude, hasIncludedChildren, true, {

so the doc is not yet populated with it's initial values.

just checking for this[field] before addToSet fixes my problem...

@diegohaz
Copy link
Owner

diegohaz commented Apr 4, 2020

@nickhingston Would you mind sending a PR?

@nickhingston
Copy link
Contributor

nickhingston commented Apr 4, 2020 via email

nickhingston added a commit to nickhingston/mongoose-keywords that referenced this issue Apr 4, 2020
@dagenius007
Copy link

Please can you merge the fix. Broke my code

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

8 participants