dman
dman

Reputation: 11064

Strange Mongoose schema.js error - `options` may not be used as a schema pathname

In my schema, if I have options in metrics : [ { options : {} } ]then I get:

/home/one/cloudimageshare-monitoring/project/node_modules/mongoose/lib/schema.js:282
    throw new Error("`" + path + "` may not be used as a schema pathname");
          ^
Error: `options` may not be used as a schema pathname

But if change options to any other word... like qoptions....then the error goes away. Why is this happening?

var mongoose = require('mongoose');
var Schema = mongoose.Schema;

var FilesystemSchema = new mongoose.Schema({
    timeStamp : { type : Date, index: true },
    avaiable : Boolean,
    status : String,
    metrics : [
        { options : {
                data : String,
                type : String,
                unit : String
               }
        },
        { freeFiles : {
              data : Number,
              type : String,
              unit : String
             }
        },
        { total : {
              data : Number,
              type : String,
              unit : String
             }
        },
        { avail : {
              data : Number,
              type : String,
              unit : String
             }
        },
        { free : {
              data : Number,
              type : String,
              unit : String
             }
        },
        { files : {
              data : Number,
              type : String,
              unit : String
             }
        },
        { used : {
              data : Number,
              type : String,
              unit : String
             }
        }
 ]   
});

module.exports = FilesystemSchema;

Upvotes: 15

Views: 10007

Answers (2)

TOPKAT
TOPKAT

Reputation: 8668

I have been having the same issue, and since we can not use field names as common as options, errors... I have disabled it like so:

new mongoose.Schema({ firstName: String }, { supressReservedKeysWarning: true })

Then, everything was working as expected.

Here is an issue in the mongoose repo on the subject Remove all reserved keywords from schema

Upvotes: 1

Stankalank
Stankalank

Reputation: 1557

Mongoose has a number of Reserved schema names that can't be used, to avoid conflicting with Mongoose's internal implementation. The list, from the docs gives the following as reserved:

on, emit, _events, db, get, set, init, isNew, errors, schema, options, modelName, collection, _pres, _posts, toObject

These terms should be avoided in your schema!

Upvotes: 55

Related Questions