title: Mongoose model.methods error: "TypeError: Cannot convert undefined or null to object"
body: <p>First of all I am really sorry if it is a repeated pos, but I really didn't find a solution for this. Also, I don't have much experience with mongo/express (using mea stack), so my apologies if it is a basic question or I forgot some important information.
  I am having the following error when trying to compile my app:</p>
  <blockquote>
  <p>C:\Users\backend\node_modules\node-restful\lib\model.js:89
  delete self.routes[method];
  ^</p>
  <p>TypeError: Cannot convert undefined or null to object
  at C:\Users\backend\node_modules\node-restful\lib\model.js:89:24
  at Array.forEach ()
  at Function.Model.methods (C:\Users\backend\node_modules\node-restful\lib\model.js:88:11)
  at Object. (C:\Users\backend\API\sensor\measure\measureSensorService.js:4:15)
  at Module._compile (internal/modules/cjs/loader.js:1085:14)
  at Object.Module._extensions..js (internal/modules/cjs/loader.js:1114:10)
  at Module.load (internal/modules/cjs/loader.js:950:32)
  at Function.Module._load (internal/modules/cjs/loader.js:790:14)
  at Module.require (internal/modules/cjs/loader.js:974:19)
  at require (internal/modules/cjs/helpers.js:92:18)
  at Object. (C:\Users\backend\config\routes.js:6:30)
  at Module._compile (internal/modules/cjs/loader.js:1085:14)
  at Object.Module._extensions..js (internal/modules/cjs/loader.js:1114:10)
  at Module.load (internal/modules/cjs/loader.js:950:32)
  at Function.Module._load (internal/modules/cjs/loader.js:790:14)
  at Module.require (internal/modules/cjs/loader.js:974:19)</p>
  </blockquote>
  <p>And here is my code:</p>
  <p>routes.js</p>
  <pre><code>const express = require('express')
  
  const workGroupService = require('../API/workGroup/workGroupService')
  const workerService = require('../API/worker/workerService')
  const locationService = require('../API/location/locationService')
  const measureSensorService = require('../API/sensor/measure/measureSensorService')
  const locationSensorService = require('../API/sensor/location/locationSensorService')
  const heatMeasureContext = require('../API/context/measureContext/heatMeasureContext/heatMeasureContextService')
  const ruidMeasureContext = require('../API/context/measureContext/ruidMeasureContext/ruidMeasureContextService')
  const workerLocationContext = require('../API/context/workerLocationContext/workerLocationContextService')
  
  module.exports = function(server) {
  
    //APIs routes
    const router = express.Router()
    server.use('/api', router)
  
    workGroupService.register(router, '/workGroup')
    workerService.register(router, '/worker')
    locationService.register(router, '/location')
    measureSensorService.register(router, '/measureSensor')
    locationSensorService.register(router, '/locationSensor')
    heatMeasureContext.register(router, '/heatMeasureContext')
    ruidMeasureContext.register(router, '/ruidMeasureContext')
    workerLocationContext.register(router, '/workerLocationContext')
  }
  </code></pre>
  <p>measureSensorService.js</p>
  <pre><code>const _ = require('lodash')
  const MeasureSensor = require('./measureSensor')
  
  MeasureSensor.methods(['get', 'post', 'put', 'delete'])
  MeasureSensor.updateOptions({new: true, runValidators: true})
  </code></pre>
  <p>measureSensor.js</p>
  <pre><code>const { Schema } = require('mongoose')
  const restful = require('node-restful')
  const mongoose = restful.mongoose
  
  const Sensor = require('../sensor')
  
  const measureSensor = Sensor.discriminator('MeasureSensor', new mongoose.Schema({
    measurerType: { type: String, required: false, uppercase: true,
      enumValues: ['HEAT', 'RUID'] }
  }))
  
  module.exports = restful.model('MeasureSensor')
  </code></pre>
  <p>I read something in the internet about lowering the version of mongoose from 5 to 4, but I tried and didn't work. Here is my package.json dependencies:</p>
  <pre><code>&quot;dependencies&quot;: {
    &quot;body-parser&quot;: &quot;^1.19.0&quot;,
    &quot;express&quot;: &quot;^4.17.1&quot;,
    &quot;express-query-int&quot;: &quot;^3.0.0&quot;,
    &quot;lodash&quot;: &quot;^4.17.21&quot;,
    &quot;mongoose&quot;: &quot;^5.13.9&quot;,
    &quot;mongoose-paginate&quot;: &quot;^5.0.3&quot;,
    &quot;node-restful&quot;: &quot;^0.2.6&quot;
  },
  </code></pre>
  <p>I am really lost here, since this issue is happening in the middle of my &quot;routes.js&quot;, and my previous routes contain the same service code and seems to work fine.</p>
  <p>Please, can you help me?</p>
  <p>Thanks in advance!</p>
  <h2>Update</h2>
  <p>I've done some more analysis and I understand that this issue is happening due to the use of <em>mongoose discriminators</em> with the <em>node-restful</em> npm module. Still can't solve though. I'm open to alternative options or other ideas to solve the issue. Anyone?</p>