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);
});
}