Cannot read properties of undefined (reading 'name') when posting to mongoose
ERROR:
Cannot read properties of undefined (reading 'name')
router.post("/", async (req, res) => {
const newItem = new items({
name: req.body.name
})
try {
const submitted = await items.save();
console.log(submitted);
}
catch (err) {
console.log(err)
}
})
Schema:
const mongoose = require("mongoose");
const pieces = new mongoose.Schema({
name: String
})
module.exports = mongoose.model("items", pieces);
//index
const express = require("express");
const app = express();
const mongoose = require("mongoose");
require("dotenv/config");
const product = require("../backend/Router/productRoutes");
app.use("/add", product);
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
mongoose.connect(process.env.MONGO_DB, () => {
console.log("DB CONNECTED")
});
app.listen(3500, () => {
console.log("server up");
});
this same code works in another project so I'm confused as to what's going on here. I tried almost every suggestion but nothing works.
Comments
Post a Comment