janez89 / mongoose-materialized

mongoose materialized plugin
59 stars 22 forks source link

Saving a parentId that points to non-existing document results in an uncontrolled crash #39

Open DenBo opened 4 years ago

DenBo commented 4 years ago

Expected behaviour: When saving a document with id in parentId field that doesn't point to any document in database, document validation should fail with descriptive error message

Current behaviour: When saving a document with id in parentId field that doesn't point to any document in database, the app crashes with a very non-descriptive error

Steps to recreate:

  1. Create a new project with npm init and configure package.json
  2. Install mongoose and mongoose-materialized with npm
{
  "name": "test-project",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "start": "node index"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "mongoose": "^5.8.7",
    "mongoose-materialized": "^0.2.0"
  }
}
  1. Create model:
const mongoose = require('mongoose')
const materializedPlugin = require('mongoose-materialized')

const { Schema } = mongoose

const TestSchema = new Schema({
  name: {
    type: String,
    required: 'missing_name'
  }
})

TestSchema.plugin(materializedPlugin)

mongoose.model('Test', TestSchema)
  1. Create index file:
const { resolve } = require('path')
const mongoose = require('mongoose')

require(resolve('./models/test.model.js'))

const Test = mongoose.model('Test')

const run = async () => {
  await mongoose.connect(`mongodb://localhost:27017`, {
    dbName: 'test-project',
    useNewUrlParser: true
  })

  const testDoc = new Test({
    name: 'test name',
    parentId: mongoose.Types.ObjectId()
  })

  await testDoc.save()
}

run()
  1. Run index file

Result:

➜  test-project npm start

> test-project@1.0.0 start /Users/user/Documents/projects/test-project
> node index

(node:439) DeprecationWarning: current Server Discovery and Monitoring engine is deprecated, and will be removed in a future version. To use the new Server Discover and Monitoring engine, pass option { useUnifiedTopology: true } to the MongoClient constructor.
(node:439) DeprecationWarning: collection.ensureIndex is deprecated. Use createIndexes instead.
events.js:183
      throw er; // Unhandled 'error' event
      ^

TypeError: done is not a function
    at /Users/user/Documents/projects/test-project/node_modules/mongoose-materialized/lib/materialized.js:142:28
    at /Users/user/Documents/projects/test-project/node_modules/mongoose/lib/model.js:4784:16
    at /Users/user/Documents/projects/test-project/node_modules/mongoose/lib/utils.js:276:16
    at /Users/user/Documents/projects/test-project/node_modules/mongoose/lib/model.js:4803:21
    at _hooks.execPost (/Users/denisb/Documents/projects/test-project/node_modules/mongoose/lib/query.js:4364:11)
    at /Users/user/Documents/projects/test-project/node_modules/kareem/index.js:135:16
    at _combinedTickCallback (internal/process/next_tick.js:132:7)
    at process._tickCallback (internal/process/next_tick.js:181:9)
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! test-project@1.0.0 start: `node index`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the test-project@1.0.0 start script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     /Users/user/.npm/_logs/2020-01-17T12_16_06_157Z-debug.log

Node version: 8.16.2