OverwriteModelError:编译后无法覆盖`Product`模型

时间:2017-05-05 19:14:25

标签: javascript node.js mongodb express mongoose

我试图在purchaseSchema中调用我的productSchema,但是我遇到了这个错误:OverwriteModelError:编译后无法覆盖Product模型。 我该怎么办?

这是我的产品架构:

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

var productSchema = new Schema({

cd: {
    type: Number,
    require: true
},

nm_iten: {
    type: String,
    require: true
},

type_iten: {
    type: String,
    require: true
},

unity: {
    type: String,
    require: true
},

});
var product = mongoose.model('Product',productSchema);
module.exports = {product:product}

而且,这是我的购买架构:

   var mongoose = require('mongoose');
   var Schema = mongoose.Schema;
   var productSchema = require('./products');
   var product = productSchema.product;

   var purchaseSchema = new Schema({

     code: {
       type: Number,
       require: true
    },

     product:[product],

     quant: {
       type: Number,
       require: true
    },

    vl_uni: {
       type: Number,
       require: true
     },

     vl_total: {
        type: Number,
        require: true
    }


    });
    mongoose.model('Purchase',purchaseSchema);

1 个答案:

答案 0 :(得分:0)

在购买模式中,product需要指向productSchema,而不是产品型号。

产品架构:

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

var productSchema = new Schema({
    cd: {
        type: Number,
        require: true
    },    
    nm_iten: {
        type: String,
        require: true
    },       
    type_iten: {
        type: String,
        require: true
    },        
    unity: {
        type: String,
        require: true
    },   
});
module.exports = productSchema;

购买架构:

var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var productSchema = require('./products');
var product = productSchema;

var purchaseSchema = new Schema({
   code: {
       type: Number,
       require: true
   },
   product:[product],
   quant: {
       type: Number,
       require: true
   },
   vl_uni: {
       type: Number,
       require: true
   },
   vl_total: {
       type: Number,
       require: true
   }
});
mongoose.model('Purchase',purchaseSchema);

有关更多信息,请参阅有关嵌入式文档的mongodb文档: http://mongoosejs.com/docs/2.7.x/docs/embedded-documents.html