2017-05-05 43 views
1

我試圖在我的purchaseSchema中調用我的productSchema,但是我得到了這個錯誤:OverwriteModelError:編譯後無法覆蓋Product模型。 我可以嘗試什麼?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); 

回答

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