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

[BUG] this.increment is not a function #64

Open
RuudVerhoef opened this issue Nov 6, 2019 · 10 comments · Fixed by #155
Open

[BUG] this.increment is not a function #64

RuudVerhoef opened this issue Nov 6, 2019 · 10 comments · Fixed by #155
Labels

Comments

@RuudVerhoef
Copy link

I just added the plugin using:
const { updateIfCurrentPlugin } = require('mongoose-update-if-current');
// insert mongoose plugins
mongoose.plugin(updateIfCurrentPlugin);
In the file where my server is declared. If I understood the manual correctly this should enable it.

However when I try to add any new document I currently receive the following error in Postman:

{
    "message": "this.increment is not a function",
    "error": {}
}

I am using the latest version of Mongoose & MongoDB.

@RuudVerhoef RuudVerhoef added the bug label Nov 6, 2019
@eoin-obrien
Copy link
Owner

eoin-obrien commented Nov 6, 2019 via email

@RuudVerhoef
Copy link
Author

I am not using any transpilers. My NodeJS version is 10.16.0. I can try to reproduce it later today or tomorrow using a minimal example. I hoped this might had been a known issue resulting from wrong usage.

@adriano-tirloni
Copy link

I also had this issue in a nested schema when using the global setup;
There is not much to it, below is the basic setup. Since there is no __v in the subdocument I believe that is where the problem is.

When I have time I can write a small repo reproducing.

"mongoose": "^5.7.8", "mongoose-update-if-current": "^1.3.2",

const aliasSchema   = new mongoose.Schema({
    name: {type: String}
},{ timestamps: true })

const userSchema   = new mongoose.Schema({
    email: {type: String, unique: true},
    aliases: [aliasSchema]
},{ timestamps: true })

@ynaciri
Copy link

ynaciri commented Jan 21, 2020

I could reproduce it with using different options between the main schemas and nested schema.
Having different versionKey between schemas can trigger the same error.
In my case, I configured the plugin globally and wanted to use versioning only on the main schemas with the following options :
{ toObject: { virtuals: true }, timestamps: true, versionKey: 'v' }

The nested schemas used default options, hence the versionKey name was '__v' and it never found an increment method on any of them.

I tried disabling versioning on nested schema with options { versionKey: false } but that triggers the error "document schema must have a version key".

So I guess we can't use a global plugin configuration if some schemas won't be versioned, right ? Is there any way to simply exclude nested schema from being versioned by the plugin ?

Nainterceptor added a commit to Qonfucius/mongoose-update-if-current that referenced this issue Apr 2, 2020
While we have a mongoose schema with a subdocument, it's expected to don't add hooks to this internal document, or it will cause errors.

Fix eoin-obrien#64
@Nainterceptor
Copy link

Ok, I've the same issue, blocking our feature development, I've forked the repo, fixed the issue then submited a pull request.
@eoin-obrien If you cannot check & accept this PR and release in following days, I'll fork npm too, for our production usage.

@eoin-obrien
Copy link
Owner

@Nainterceptor Merged, thanks!

@eoin-obrien
Copy link
Owner

The fix is live on npm as version 1.4.0 now.

@chiubaka
Copy link

chiubaka commented Aug 4, 2020

I believe I've just encountered this issue while running version 1.4.0! Everything works OK if I explicitly add this plugin to the top-level models, but global configuration is a no go, unfortunately :/.

@eoin-obrien
Copy link
Owner

@chiubaka Thanks for flagging this, I'll start investigating! Could you share the repo or the code snippets to reproduce the issue?

@chiubaka
Copy link

chiubaka commented Aug 6, 2020

Hello! Thanks for your fast response. Unfortunately, the repo in question is private and proprietary... I'll try to set up a repro repo for you in the next week, if I can find a little time to do so.

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

Successfully merging a pull request may close this issue.

6 participants