我是 Mongoose 的新手,正在 Web Developer Bootcamp(Udemy 上的那個)中從事一個小專案,突然遇到了 Mongoose 的這個問題。我覺得這實際上是一個非常簡單的修復,但這是我在 index.js 中的 Express 路由:
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}`)
})
我沒有包括其他路線,因為它們運行良好。這是新的.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>
當我使用瀏覽器訪問 localhost:8080/products/new 時,終端出現此錯誤:
/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)
at model.Query.Query.then (/home/christian-js/Code/MongoExpress/node_modules/mongoose/lib/query.js:4818:15)
at processTicksAndRejections (node:internal/process/task_queues:96:5) {
messageFormat: undefined,
stringValue: '"new"',
kind: 'ObjectId',
value: 'new',
path: '_id',
reason: BSONTypeError: Argument passed in must be a string of 12 bytes or a string of 24 hex characters or an integer
at new BSONTypeError (/home/christian-js/Code/MongoExpress/node_modules/bson/lib/error.js:41:28)
at new ObjectId (/home/christian-js/Code/MongoExpress/node_modules/bson/lib/objectid.js:67:23)
at castObjectId (/home/christian-js/Code/MongoExpress/node_modules/mongoose/lib/cast/objectid.js:24:12)
at ObjectId.cast (/home/christian-js/Code/MongoExpress/node_modules/mongoose/lib/schema/objectid.js:245:12)
at ObjectId.SchemaType.applySetters (/home/christian-js/Code/MongoExpress/node_modules/mongoose/lib/schematype.js:1189:12)
at ObjectId.SchemaType._castForQuery (/home/christian-js/Code/MongoExpress/node_modules/mongoose/lib/schematype.js:1623:15)
at ObjectId.SchemaType.castForQuery (/home/christian-js/Code/MongoExpress/node_modules/mongoose/lib/schematype.js:1613:15)
at ObjectId.SchemaType.castForQueryWrapper (/home/christian-js/Code/MongoExpress/node_modules/mongoose/lib/schematype.js:1590:20)
at cast (/home/christian-js/Code/MongoExpress/node_modules/mongoose/lib/cast.js:344:32)
at model.Query.Query.cast (/home/christian-js/Code/MongoExpress/node_modules/mongoose/lib/query.js:5141:12),
valueType: 'string'
}
就像我說的,我是 Mongoose 的新手,所以我不知道該怎么做。我希望我添加了足夠的細節,但如果沒有,我會更新這個問題。
uj5u.com熱心網友回復:
嘗試這個:
const newProduct = await new Product(req.body).save()
res.redirect(`/products/${newProduct._id}`)
在創建新產品時,您的論點似乎有問題。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/485266.html
標籤:javascript 休息 表示 猫鼬 本地主机
