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

mongoose validation error (in hook) kills nodejs process #222

Closed
wkamir opened this issue Nov 16, 2017 · 3 comments
Closed

mongoose validation error (in hook) kills nodejs process #222

wkamir opened this issue Nov 16, 2017 · 3 comments

Comments

@wkamir
Copy link

wkamir commented Nov 16, 2017

Steps to reproduce

  • create a schema which has a sub-schema which has a sub-schema (this is important otherwise it is not reproducible)
const mongoose = require('mongoose');

var subItemSchema = new mongoose.Schema({
  text: { type: String, required: true },
  value: { type: Number, required: true }
}, {_id: false });

var itemSchema = new mongoose.Schema({
  subItems: [subItemSchema]
}, {_id: false });

var schema = new mongoose.Schema({
  items: [itemSchema]
}, {collection: 'testcollection'});

module.exports = mongoose.model('TestModel', schema);
  • create a 'before' hook
app.use('/api/thestuff', service({
    Model: models.TheStuff
  }));

  const drugService = app.service('/api/thestuff');

  drugService.hooks({
    before: {
      patch: function(context) {
        return context.service.update(context.id, {
          $push: {
            "items": { "subItems": [ "this is invalid" ] }
          }
        }).then(res => context);
      }
    }
  })
  • do a HTTP PATCH call which then executes the hook (I use postman for that)

Expected behavior

Feathers returns gracefully (with a validation error message and HTTP 400 Bad Request)

Actual behavior

NodeJs process gets killed:

c:\Development\TheApp\backend\node_modules\mongoose\lib\types\embedded.js:222
    throw err;
    ^
ValidationError: Validation failed: subItems.0.value: Path `value` is required., subItems.0.text: Path `text` is required.
    at new ValidationError (c:\Development\TheApp\backend\node_modules\mongoose\lib\error\validation.js:28:11)
    at EmbeddedDocument.Document.invalidate (c:\Development\TheApp\backend\node_modules\mongoose\lib\document.js:1658:32)
    at EmbeddedDocument.invalidate (c:\Development\TheApp\backend\node_modules\mongoose\lib\types\embedded.js:218:35)
    at EmbeddedDocument.invalidate (c:\Development\TheApp\backend\node_modules\mongoose\lib\types\embedded.js:229:19)
    at c:\Development\TheApp\backend\node_modules\mongoose\lib\document.js:1530:17
    at validate (c:\Development\TheApp\backend\node_modules\mongoose\lib\schematype.js:784:7)
    at c:\Development\TheApp\backend\node_modules\mongoose\lib\schematype.js:829:11
    at Array.forEach (<anonymous>)
    at SchemaNumber.SchemaType.doValidate (c:\Development\TheApp\backend\node_modules\mongoose\lib\schematype.js:789:19)
    at c:\Development\TheApp\backend\node_modules\mongoose\lib\document.js:1528:9
    at _combinedTickCallback (internal/process/next_tick.js:131:7)
    at process._tickCallback (internal/process/next_tick.js:180:9)
Waiting for the debugger to disconnect...
ValidationError

System configuration

Tell us about the applicable parts of your setup.

Module versions (especially the part that's not working):

    "@feathersjs/configuration": "^1.0.0",
    "@feathersjs/errors": "^3.0.0",
    "@feathersjs/express": "^1.1.1",
    "@feathersjs/feathers": "^3.0.0",
    "compression": "^1.7.1",
    "cors": "^2.8.4",
    "feathers-hooks-common": "^3.10.0",
    "feathers-mongoose": "^5.1.2",
    "helmet": "^3.9.0",
    "mongoose": "^4.13.3",
    "qrcode": "^1.0.0",
    "serve-favicon": "^2.4.5",
    "winston": "^2.4.0"

NodeJS version: v8.9.1

Operating System: Windows 10

@daffl
Copy link
Member

daffl commented Nov 16, 2017

This looks like a Mongoose error. Does this happen if you run the same query directly on the Mongoose model?

@wkamir
Copy link
Author

wkamir commented Nov 17, 2017

Hi daffl,

yes you are right - I also ralized (by now) that it is Mongoose itself that is throwing the exception (and probably on a callback so it bleeds through right to the event loop and kills the process).

Thanks for taking the time - I will just raise the issue on the mongoose project...

@wkamir wkamir closed this as completed Nov 17, 2017
@PManager1
Copy link

@wkamir im seeing same issue. Any update on this issue ?

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