node.js - 奇怪的 Mongoose schema.js 错误 - `options` 不能用作模式路径名

标签 node.js mongodb mongoose

在我的架构中,如果我有 optionsmetrics : [ { options : {} } ]那么我得到:

/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

但是,如果将 options 更改为任何其他单词...例如 qoptions...。那么错误就会消失。为什么会这样?

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;

最佳答案

Mongoose 有许多不能使用的 Reserved 架构名称,以避免与 Mongoose 的内部实现发生冲突。该列表来自 docs保留以下内容:

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

在您的架构中应避免使用这些术语!

关于node.js - 奇怪的 Mongoose schema.js 错误 - `options` 不能用作模式路径名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24130600/

相关文章:

javascript - 出于 (A)GPL 的目的,require()-ing node.js 中的库是否使我的代码成为 "derived work"?

node.js - 由于外部模型, Hook (预中间件)中的 Mongoose 模型查询(findById)错误

node.js - Mongoose 按半径查找地理点

node.js - 如何在 Node 中触发 Mongoose 保存的套接字事件?

javascript - 在 argv[2] NodeJs 之后连接参数

node.js - 运行 grunt-usemin 时收到错误消息 "No "连接“找到目标”

node.js - Node.js 如何知道在views 文件夹中渲染ejs

mongodb - 在 Meteor.js 中,我如何让两个开发项目使用同一个 Mongo 实例?

javascript - 如何在nestjs中转换api响应?

javascript - 如何处理与 MongoDB 和 node.js 的异步?