CastError: Cast to ObjectId failed for value "new" (type string) at path "_id" for model "Model"

CastError: Cast to ObjectId failed for value "new" (type string) at path "_id" for model "Model"

我是 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}`)
})

我没有包括其他路线,因为它们运行良好。这里是 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>

当我使用浏览器访问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 的新手,所以我不知道该怎么做。我希望我添加了足够的细节,但如果没有,我会更新这个问题。

试试这个:

const newProduct = await new Product(req.body).save()
res.redirect(`/products/${newProduct._id}`)

您在创建新产品时似乎遇到了问题。