Mongoose: Use "hook" as a field in a Schema

Created on 7 Mar 2017  路  4Comments  路  Source: Automattic/mongoose

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

What is the current behavior?
this.hook is not a function

If the current behavior is a bug, please provide the steps to reproduce.
Create a schema like this:

const Schema = new Schema({
    name: String,
    hook: String,
})

Then try and save a new document:

new Model({ 
    name: 'test', 
    hook: 'test' 
}).save()

You get the error:

TypeError: this.hook is not a function
    at model._lazySetupHooks (/admin/node_modules/hooks-fixed/hooks.js:177:12)
    at model.pre (/admin/node_modules/hooks-fixed/hooks.js:125:10)
    at /admin/node_modules/mongoose/lib/document.js:1831:17
    at Array.forEach (native)
    at /admin/node_modules/mongoose/lib/document.js:1829:26
    at Array.forEach (native)
    at model.Document.$__registerHooksFromSchema (/admin/node_modules/mongoose/lib/document.js:1795:23)
    at model.Document (/admin/node_modules/mongoose/lib/document.js:76:8)
    at model.Model (/admin/node_modules/mongoose/lib/model.js:42:12)
    at new model (/admin/node_modules/mongoose/lib/model.js:3060:11)

I've tried the Schema:

const Schema = new Schema({
    name: String,
    hook: { type: String },
})

But it yields the same result.

What is the expected behavior?
For my mongo document document to end up looking like:

{
    "name": "test",
    "hook": "test"
}
confirmed-bug

Most helpful comment

Okay, in the mean time I'll rename the variable to hooker. 馃槃

All 4 comments

I actually get a different error:

Error: TypeError: Cannot read property 'scope' of undefined
TypeError: Cannot read property 'scope' of undefined
    at model.get [as hook] (/Users/roonie/workspace/oss/mongoose/gh-5047/node_modules/mon
goose/lib/document.js:1834:38)
    at model._lazySetupHooks (/Users/roonie/workspace/oss/mongoose/gh-5047/node_modules/h
ooks-fixed/hooks.js:177:11)
    at model.pre [as $pre] (/Users/roonie/workspace/oss/mongoose/gh-5047/node_modules/hoo
ks-fixed/hooks.js:125:10)
    at applyHooks (/Users/roonie/workspace/oss/mongoose/gh-5047/node_modules/mongoose/lib
/services/model/applyHooks.js:165:28)
    at Function.compile (/Users/roonie/workspace/oss/mongoose/gh-5047/node_modules/mongoo
se/lib/model.js:3332:3)
    at Mongoose.model (/Users/roonie/workspace/oss/mongoose/gh-5047/node_modules/mongoose
/lib/index.js:408:22)
    at exec (/Users/roonie/workspace/oss/mongoose/gh-5047/index.js:20:26)
    at Object.<anonymous> (/Users/roonie/workspace/oss/mongoose/gh-5047/index.js:7:3)
    at Module._compile (module.js:570:32)
    at Object.Module._extensions..js (module.js:579:10)
    at Module.load (module.js:487:32)
    at tryModuleLoad (module.js:446:12)
    at Function.Module._load (module.js:438:3)
    at Module.runMain (module.js:604:10)
    at run (bootstrap_node.js:394:7)
    at startup (bootstrap_node.js:149:9)

with the following script

const mongoose = require('mongoose');
mongoose.Promise = global.Promise;

const GITHUB_ISSUE = `gh-5047`;

try {
  exec();
} catch (error) {
  console.error(`Error: ${ error }\n${ error.stack }`);
}

function exec() {
  const db = mongoose.connect(`mongodb://localhost:27017/${ GITHUB_ISSUE }`);

  const schema = new mongoose.Schema({
    name: String,
    hook: { type: String },
  });

  const Model = mongoose.model('Model', schema);

  new Model({
    name: 'test',
    hook: 'test'
  }).save();
}

My guess is that hook is a reserved word and you should probably use a different name for your schema property

Okay, in the mean time I'll rename the variable to hooker. 馃槃

@vkarpov15 thank you for your time on this! Could you just post in here when this fix is released in the next version?

Sure will do, should be tomorrow. Also, love the variable name, that cracks me up :joy:

Was this page helpful?
0 / 5 - 0 ratings