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] reserved schema keys don't throw when type is nested object #8869

Closed
valiant1x opened this issue Apr 26, 2020 · 1 comment · Fixed by #8875
Closed

[Bug] reserved schema keys don't throw when type is nested object #8869

valiant1x opened this issue Apr 26, 2020 · 1 comment · Fixed by #8875
Assignees
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Milestone

Comments

@valiant1x
Copy link

Do you want to request a feature or report a bug?
bug

What is the current behavior?

Passing the Schema constructor an object with the key errors causes a very obscure, non-obvious error to be thrown only once an associated Document is save()d or create()d, producing an error about priorDoc being undefined:

(node:6930) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'priorDoc' of undefined
    at /home/node/qproxy-api/node_modules/mongoose/lib/document.js:1118:27
    at model.$set (/home/node/qproxy-api/node_modules/mongoose/lib/document.js:1125:5)
    at model.set [as errors] (/home/node/qproxy-api/node_modules/mongoose/lib/helpers/document/compile.js:137:20)
    at model.Document (/home/node/qproxy-api/node_modules/mongoose/lib/document.js:85:15)
    at model.Model (/home/node/qproxy-api/node_modules/mongoose/lib/model.js:105:12)
    at new model (/home/node/qproxy-api/node_modules/mongoose/lib/model.js:4669:15)
    at /home/node/qproxy-api/node_modules/mongoose/lib/model.js:3106:22
    at /home/node/qproxy-api/node_modules/mongoose/lib/model.js:3142:7
    at Array.forEach (<anonymous>)
    at /home/node/qproxy-api/node_modules/mongoose/lib/model.js:3141:15
    at /home/node/qproxy-api/node_modules/mongoose/lib/helpers/promiseOrCallback.js:31:5
    at new Promise (<anonymous>)
    at promiseOrCallback (/home/node/qproxy-api/node_modules/mongoose/lib/helpers/promiseOrCallback.js:30:10)
    at Function.create (/home/node/qproxy-api/node_modules/mongoose/lib/model.js:3076:10)
    at Object.<anonymous> (/home/node/qproxy-api/test.js:11:7)
    at Module._compile (internal/modules/cjs/loader.js:1158:30)
(node:6930) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:6930) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

If the current behavior is a bug, please provide the steps to reproduce.

const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const Phone = new Schema({
  name: String,
  errors: {
      numErrors: Number,
      lastError: Date
   }
}, {timestamps: true});
var Phone = mongoose.model('Phone', Phone);
Phone.create({ name: 'test' });

What is the expected behavior?

Ideally, the user should see an error indicating that a restricted keyword was used, or indicate some issue in the Schema constructor. It was very hard to track down the underlying issue in the current format. Searching docs and even google for priorDoc undefined yields nothing useful. After scouring the docs I noticed prototype.errors being some sort of reserved keyword.

What are the versions of Node.js, Mongoose and MongoDB you are using? Note that "latest" is not a version.

Mongoose 5.9.10
NodeJS 12.16.1
MongoDB 4.2.6

@AbdelrahmanHafez
Copy link
Collaborator

errors is a reserved keyword, you're right, the error should be thrown. on compiling the schema, I just tested, and it works with flat types, but not with nested objects.

'use strict';
const mongoose = require('./');
const { Schema } = mongoose;
const assert = require('assert');

assert.throws(
  buildInvalidSchema,
  { name: 'Error', message: /`errors` may not be used as a schema pathname/ }
);

function buildInvalidSchema() {
  new Schema({
    errors: {
      nested: String
    }
  });
}

@vkarpov15 can we change the issue title to [Bug] reserved schema keys don't throw when type is nested object?

@AbdelrahmanHafez AbdelrahmanHafez added the confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. label Apr 27, 2020
@AbdelrahmanHafez AbdelrahmanHafez added this to the 5.9.12 milestone Apr 27, 2020
@AbdelrahmanHafez AbdelrahmanHafez self-assigned this Apr 27, 2020
AbdelrahmanHafez added a commit to AbdelrahmanHafez/mongoose that referenced this issue Apr 27, 2020
@valiant1x valiant1x changed the title Schema definition params fail obscurely when using reserved object key errors [Bug] reserved schema keys don't throw when type is nested object Apr 27, 2020
vkarpov15 added a commit to AbdelrahmanHafez/mongoose that referenced this issue Apr 30, 2020
vkarpov15 added a commit that referenced this issue Apr 30, 2020
throw error on schema with reserved key with type of object
@vkarpov15 vkarpov15 reopened this May 3, 2020
AbdelrahmanHafez added a commit to AbdelrahmanHafez/mongoose that referenced this issue Jul 9, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants