猫鼬在定义字段两次时会引发“字段不在架构中”错误



我正在使用Node v0.10.31和mongoose@3.8.22。

我想我遇到了一个在特定事情发生时出现的错误。此错误的含义使我无法在同一架构上具有字段"name"和"father.name.full"。

这就是我定义架构的方式:

'use strict';
var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/myapp');
var PersonSchema = new mongoose.Schema({
    name: {
        type: mongoose.Schema.Types.ObjectId,
        ref: 'Name', // if this is commented out, no errors
        required: false,
        default: null,
    },
    father: {
        name: { full: String } // if this is `name: String`, no errors
    }
}, {
    strict: 'throw' // if this is commented out, no errors
});
var Person = mongoose.model('Person', PersonSchema);

这是我创建文档的方式:

var object2save = {
    name: mongoose.Types.ObjectId(),
    father: {
        name: {
            full: 'father full name'
        }
    }
}
var doc = new Person(object2save); // this throws the error

错误和堆栈跟踪是

Error: Field `name` is not in schema.
    at model.Document.set (/PATH/node_modules/mongoose/lib/document.js:469:19)
    at model.Document.set (/PATH/node_modules/mongoose/lib/document.js:464:16)
    at model.Document (/PATH/node_modules/mongoose/lib/document.js:60:10)
    at model.Model (/PATH/node_modules/mongoose/lib/model.js:43:12)
    at new model (/PATH/node_modules/mongoose/lib/model.js:2535:11)
    at Object.<anonymous> (/PATH/Desktop/node/test-mongoose/path-type-mismatch.js:43:11)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)

这实际上是一个错误还是一个功能?如果它是一个功能,那么它似乎是对架构设计的限制。我打算在 GitHub 上为 Mongoose 创建一个问题,但我想我应该先问 Stack Overflow 以确定:-)

这个问题似乎是猫鼬中的一个错误。该问题已修复,并将根据此链接在猫鼬 3.8.24 中发布:

https://github.com/LearnBoost/mongoose/issues/2665

最新更新