两次定义字段时,猫鼬抛出“字段不在架构中"错误 [英] Mongoose throws `Field is not in schema` error when defining a field twice

查看:59
本文介绍了两次定义字段时,猫鼬抛出“字段不在架构中"错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am using Node v0.10.31 and mongoose@3.8.22.

我认为我遇到了在发生特定情况时出现的错误.此错误的含义使我无法在同一架构上使用字段名称"和"father.name.full".

I think I have encountered a bug that appears when specific things happen. The implications of this bug prevents me from having a field "name" and "father.name.full" on the same schema.

这是我定义模式的方式:

This is how I define my schema:

'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);

这是我创建文档的方式:

This is how I am creating the document:

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以确保:-)

Is this actually a bug or is it a feature? If it is a feature, then it seems like a limitation on schema design. I was going to create an issue on GitHub for Mongoose, but I figured I should ask Stack Overflow first to be sure :-)

推荐答案

此问题似乎是Mongoose中的错误.该问题已修复,并将根据此链接在Mongoose 3.8.24中发布:

This issue seems to be a bug in Mongoose. The issue is fixed and will be released in Mongoose 3.8.24 according to this link:

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

这篇关于两次定义字段时,猫鼬抛出“字段不在架构中"错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆