How to get rid of Error: “OverwriteModelError: Cannot overwrite `undefined` model once compiled.”?

后端 未结 6 1827
太阳男子
太阳男子 2021-02-05 07:34

I have a common method for updating document of any collection in MongoDB?

The following code is in file name Deleter.js

module.exports         


        
6条回答
  •  太阳男子
    2021-02-05 08:22

    I found it better to avoid global and exception handing-

    var mongoose = require("mongoose");
    var _ = require("underscore");
    
    var model;
    if (_.indexOf(mongoose.modelNames(), "Find")) {
        var CategorySchema = new mongoose.Schema({
            name: String,
            subCategory: [
                {
                    categoryCode: String,
                    subCategoryName: String,
                    code: String
                }
            ]
        }, {
            collection: 'category'
        });
        model = mongoose.model('Category', CategorySchema);
    }
    else {
        model = mongoose.model('Category');
    }
    
    
    module.exports = model;
    

提交回复
热议问题