I'm currently diving into Mongoose for a project in the Web Developer Bootcamp (the one on Udemy) and encountered an issue with it. It seems like a simple fix, but here are the Express routes included in index.js:
app.get('/products/new', (req, res) => {
res.render('products/new', { categories });
});
app.post('/products', async (req, res) => {
const newProduct = await new Product(req, body);
await newProduct.save();
res.redirect(`/products/${newProduct._id}`)
})
The other routes are working fine so I omitted them. Here is new.ejs:
<body>
<h1>Add A Product</h1>
<form action="/products" method="post">
label for="name">Product Name</label>
<input type="text" name="name" id="name" placeholder="product name">
<label for="price">Price (Unit)</label>
<input type="number" id="price" name="price" placeholder="price">
<label for="category">Select Category</label>
<select name="category" id="category">
<% for(let category of categories){ %>
<option value="<%=category%>">
<%=category%>
</option>
<% } %>
</select>
<button>Submit</button>
</form>
</body>
When trying to access localhost:8080/products/new using the browser, this error pops up in the terminal:
/home/christian-js/Code/MongoExpress/node_modules/mongoose/lib/query.js:4719
const castError = new CastError();
^
CastError: Cast to ObjectId failed for value "new" (type string) at path "_id" for model "Product"
at model.Query.exec (/home/christian-js/Code/MongoExpress/node_modules/mongoose/lib/query.js:4719:21)
...
Being new to Mongoose, I'm unsure how to proceed from here. If more information is needed, please let me know and I will update the question.