Im trying to build a car selling website with MERN but while building the backed it sems that my .create function is not working even though the imports seem to be correct here is the error with the files:-
const newCar = await Car.create({
^
TypeError: Car.create is not a function
here are the respective files:-
Car.js
const express= require('express');
const mongoose= require('mongoose');
const Car = require('../Models/Models.js');
const router = express.Router();
router.get ('/create', async function (req, res) {
const newCar = await Car.create({
name: "Porche",
model: "911",
color: "Blue",
year: 1981,
mileage: 500,
price: 200000,
})
})
module.exports = router;
index.js
const express = require('express');
const app = express();
const port = 3000
const car = require("./routes/Car");
app.use("/cars", car);
app.listen(port, ()=>{
console.log(`Server is running on port ${port}`);
});
Models.js
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const carSchema = new Schema({
name: String,
model: String,
color: String,
year: Number,
mileage: Number,
price: Number,
})
const UserSchema = new Schema({
name: String,
email: { type: String, unique: true },
password: String,
cars: [carSchema],
admin: Boolean,
})
const Car = mongoose.model('Car', carSchema)
const User = mongoose.model('User', UserSchema)
module.exports = { Car, User };