0

I'm making a Next.js app and I'm using MongoDB to store the books in the database and I am getting this error where I defined my model. Please help me :/

OverwriteModelError: Cannot overwrite bookPrices model once compiled.

Here's my code:

import axios from "axios";
const mongoose = require("mongoose");

export default function PriceAPI(req, res) {

mongoose.connect('mongodb://144.126.253.230:27017/booksDB');

const bookPrices = mongoose.model("bookPrices", {
    isbn10: { type: String, required: true },
    isbn13: { type: String, required: true },
    source: { type: String, require: true },
    price: { type: Number, required: true },
    listprice: { type: Number, required: true },
    url: { type: String, required: true },
    lastRequested: { type: String, required: false },
    lastUpdated: { type: String, required: false },
    edition: { type: String }
})

var data = {
    "isbn10": "8179922323",
    "isbn13": "9788179922323",
    "title": "Who Will Cry When You Die?"
};

var config = {
    method: 'post',
    url: 'http://144.126.253.230:8080/book',
    headers: {
        'Content-Type': 'application/json'
    },
    data: data
};

axios(config)
    .then(function (response) {
        console.log(response.data);
        res.status(200).json(response.data);

        bookPrices.find((err, books) => {
            books.forEach(book => {
                if (book.isbn10 === response.data[0].isbn10 && book.isbn10 === response.data[1].isbn10) {
                    console.log("Book already exists in DB.")
                } else if (book.isbn10 === response.data[0].isbn10 && book.isbn10 !== response.data[1].isbn10) {
                    bookPrices.insertMany([{
                        isbn10: response.data[1].isbn10,
                        isbn13: response.data[1].isbn13,
                        source: response.data[1].source,
                        price: response.data[1].price,
                        listprice: response.data[1].listPrice,
                        url: response.data[1].url,
                        edition: response.data[1].edition,
                        lastRequested: "EMPTY",
                        lastUpdated: "EMPTY"
                    }], err => {
                        if (err) {
                            console.log(err);
                        } else {
                            console.log("Book exists for amazon, added in DB for flipkart.")
                        }
                    })
                } else if (book.isbn10 !== response.data[1].isbn10 && book.isbn10 === response.data[2].isbn10) {
                    bookPrices.insertMany([{
                        isbn10: response.data[0].isbn10,
                        isbn13: response.data[0].isbn13,
                        source: response.data[0].source,
                        price: response.data[0].price,
                        listprice: response.data[0].listPrice,
                        url: response.data[0].url,
                        edition: response.data[0].edition,
                        lastRequested: "EMPTY",
                        lastUpdated: "EMPTY"
                    }], err => {
                        if (err) {
                            console.log(err);
                        } else {
                            console.log("Book exists for flipkart, added in DB for amazon.")
                        }
                    })
                } else if (book.isbn10 !== response.data[1].isbn10 && book.isbn10 !== response.data[1].isbn10) {
                    bookPrices.insertMany([{
                        isbn10: response.data[0].isbn10,
                        isbn13: response.data[0].isbn13,
                        source: response.data[0].source,
                        price: response.data[0].price,
                        listprice: response.data[0].listPrice,
                        url: response.data[0].url,
                        edition: response.data[0].edition,
                        lastRequested: "EMPTY",
                        lastUpdated: "EMPTY"
                    }, {
                        isbn10: response.data[1].isbn10,
                        isbn13: response.data[1].isbn13,
                        source: response.data[1].source,
                        price: response.data[1].price,
                        listprice: response.data[1].listPrice,
                        url: response.data[1].url,
                        edition: response.data[1].edition,
                        lastRequested: "EMPTY",
                        lastUpdated: "EMPTY"
                    }], err => {
                        if (err) {
                            console.log(err);
                        } else {
                            console.log("Book doesn't exists for both, added in DB for both sources.")
                        }
                    })
                }
            })
        })
    })
    .catch(function (error) {
        console.log(error);
    });

}

Vedansh
  • 19
  • 3
  • I guess that calling `insertMany` in a loop on the same element isn't allowed. Create an array and use `insertMany` once – Konrad Oct 01 '22 at 09:27
  • @KonradLinkowski I don't get it, can you explain? – Vedansh Oct 01 '22 at 10:00
  • 1
    Does this answer your question? [Cannot overwrite model once compiled Mongoose](https://stackoverflow.com/questions/19051041/cannot-overwrite-model-once-compiled-mongoose) – PeterJames Oct 01 '22 at 11:13
  • @PeterJames Nope, in that question the user is getting error because he is using the same model in 2 file but it's different in my case. – Vedansh Oct 01 '22 at 11:15
  • @PeterJames I think [this answer](https://stackoverflow.com/a/19051909/19013391) is relevant and I'll try this method to solve the problem and I'll let you know, I'm AFK right now. – Vedansh Oct 01 '22 at 11:31

0 Answers0