1

I am trying to save the query but It is giving me error cannot overwrite model once compiled. my models file is

const mongoose = require("mongoose")

const history = new mongoose.Schema({
    search_name: {
        type: String,
        required: true,

    },
    date: {
        type: Date,
        required: true
    }
})

module.exports = mongoose.model("history", history)

and my routes file is:

const keys = require("../config/keys")
const Bing = require('node-bing-api')({ accKey: "0112a649fd944630b0954a20ac8c71f6" });
let History = require("../Models/searchhistory")
module.exports = (app) => {

    app.get("/api/imagesearch/:searchname", (req, response) => {

        const query = req.params.searchname
        const per_page = parseInt(req.query.offset, 10)
        Bing.images(query, {
            count: 100,
            offset: 0
        }, (error, res, body) => {
            const hist = new History({

                })
                // hist.save().then(() => {

            // })
            response.send(body)
        })
    })
}

I am getting these error when running the code :

F:\image abstraction layer\node_modules\mongoose\lib\index.js:453
  throw new _mongoose.Error.OverwriteModelError(name);
  ^

OverwriteModelError: Cannot overwrite history model once compiled. at new OverwriteModelError (F:\image abstraction layer\node_modules\mongoose\lib\error\overwriteModel.js:20:11) at Mongoose.model (F:\image abstraction layer\node_modules\mongoose\lib\index.js:453:13) at Object. (F:\image abstraction layer\Models\searchhistory.js:15:27) at Module._compile (internal/modules/cjs/loader.js:688:30) at Object.Module._extensions..js (internal/modules/cjs/loader.js:699:10) at Module.load (internal/modules/cjs/loader.js:598:32) at tryModuleLoad (internal/modules/cjs/loader.js:537:12) at Function.Module._load (internal/modules/cjs/loader.js:529:3) at Module.require (internal/modules/cjs/loader.js:636:17) at require (internal/modules/cjs/helpers.js:20:18) at Object. (F:\image abstraction layer\routes\routes.js:3:15) at Module._compile (internal/modules/cjs/loader.js:688:30) at Object.Module._extensions..js (internal/modules/cjs/loader.js:699:10) at Module.load (internal/modules/cjs/loader.js:598:32) at tryModuleLoad (internal/modules/cjs/loader.js:537:12) at Function.Module._load (internal/modules/cjs/loader.js:529:3) [nodemon] app crashed - waiting for file changes before starting...

2 Answers 2

1

maybe you can solve it by changing that

module.exports = mongoose.model("history", history)

for that

module.exports = mongoose.model.history || mongoose.model("history", history)
0

You must be creating the same schema many places and complie them using the same model name this is not the correct way to do it.

const mongoose = require("mongoose");
const Schema = mongoose.Schema;

const history = new Schema({
    search_name: {
        type: String,
        required: true,

    },
    date: {
        type: Date,
        required: true
    }
})

const History = module.exports = mongoose.model("history", history) // use this History  const to access your history schema every where in your application

you can require it using the way you did in your code. but be sure to export your model after compiling. what's happening here is, you do not export your compiled model.. you just let it compile everytime you make a req. That's why the error occurs.

1
  • Good answer.I tried using wrapping the model export in a function and it worked Jan 7, 2019 at 2:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.